开发者

Memory Management,Adding objects to array

Hi friends I am using this code

-(void)searchItem:(NSMutableArray*)items
{

    if (开发者_运维百科[[[items objectAtIndex:1]objectForKey:@"Success"]isEqualToString:@"True"]) {
    NSMutableArray *searcharr=[[NSMutableArray alloc]init];
            for (int i=2; i<[items count]; i++) 
            {
                NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
                [searcharr addObject:[items objectAtIndex:i]];
                NSLog(@"data fetch array is====> /n%@",searcharr);
                [pool release];
            }

            searchItemsArray=[[NSMutableArray alloc]initWithArray:searcharr];
            [searcharr release];
            searcharr=nil;
}

I have released searchItemsArray in dealloc method.The items array i am getting from a webservice.It contains images and other data.I was using the for loop without NSAutoreleasePool,But when i used the instrument with simulator,i was getting the leak here.I just want to know that the code which i have given here with pool is correct or not.My app was also crashing as i was feeding this data and images in tableview cell.So please help me out. And one more thing should i always use NSAutorelease pool,while looping ......Thanks


if the method "-(void)searchItem:(NSMutableArray*)items" get's called many times then searchItemsArray will be allocated each time resulting a memory leak. If you call release in the dealloc that means you will release only the instance created at the last call of the method.

you could do something similar to this:

-(void)searchItem:(NSMutableArray*)items {

if ([[[items objectAtIndex:1]objectForKey:@"Success"]isEqualToString:@"True"])

{
   if( nil == searchItemsArray )
        searchItemsArray = [[NSMutableArray alloc]init];
   else
        [searchItemsArray removeAllObjects];

   for (int i=2; i<[items count]; i++) 
   {
        [searchItemsArray addObject:[items objectAtIndex:i]];
   } 

}

If you created the items array with alloc/init then after the call of "-(void)searchItem:(NSMutableArray*)items" you need to release it also.

If in a loop you create many many autoreleased objects that you will not use later then the autorelease pool would be an option.

Every time -autorelease is sent to an object, it is added to the inner-most autorelease pool. When the pool is drained, it simply sends -release to all the objects in the pool.

Autorelease pools are simply a convenience that allows you to defer sending -release until "later". That "later" can happen in several places, but the most common in Cocoa GUI apps is at the end of the current run loop cycle.

NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
for(int i = 0; i < 100; i++)
{
    NSString *string = [[[NSString alloc] init] autorelease];
    /* use the string */
}
[pool drain]; //or release if you don't plan to use the pool later.

The code wasn't compiled sorry for the possible errors.

You can find more info about autorelease pools at this link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜