A final question regarding Memory Warning

Discussion in 'iOS Programming' started by sujithkrishnan, Aug 31, 2008.

  1. sujithkrishnan macrumors 6502

    May 9, 2008
    Hi all..

    I just want to ask some final question regarding the memory management in iPhone?

    1. What is the leak in following line?
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    	NSString *MyIdentifier = @"MyIdentifier";
      	UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if (cell == nil) 
    		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]autorelease];
    [COLOR="red"]cell.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];[/COLOR]
    return cell;

    I posted the same before also.. Still i didnt get any satisfactory reply...

    2. When the following method get called?

    - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

    is it when app is over-shooting the memory allowed or when doing some illegal memory operations???
  2. supertoto macrumors newbie

    Jul 29, 2008

    1. As my app goes, you need retain your UIImage object before you use it.

    2. Acutally, according to the guide, it will be called, when the system memory is not enough, and you need to add your code to release some memory when received this message. It is invoked automatically by the system. So, nobody knows when it will be invoked...:cool:

    Hope help.
  3. sujithkrishnan thread starter macrumors 6502

    May 9, 2008
    But the thing is that the method is invoking for me soem times..after a 20 min usage of app.... (Even i checked in Malloc-Debug and find its just taking a MAX of 3 MB in runtime...)
  4. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    I can't see what would be leaking - all of the objects on the right side of the assignment should be autoreleased objects. The UITableViewCell will retain the UIImage assigned using cell.image= so there should be no need for you to retain it.
  5. sujithkrishnan thread starter macrumors 6502

    May 9, 2008
    Yeah... even i think ther is no need of "retain" which can cause more leak...

    I am getting a "leak-peak" for this line in INSTRUMENTS tool...

    I broke down the line into different , still i got leak for the line , where i alloc-initialize the "NSData"

    The INSTRUMENTS showing "CFURLResponse".. etc..etc... in details of leak
  6. ploppy macrumors newbie

    Jan 13, 2007
    NSData dataWithContentsOfURL: is known to leak. You might want to load your images some other way.

Share This Page