PDA

View Full Version : A final question regarding Memory Warning




sujithkrishnan
Sep 1, 2008, 01:50 AM
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];
}



cell.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];

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???



supertoto
Sep 1, 2008, 02:19 AM
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];
}



cell.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];

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???


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.

sujithkrishnan
Sep 1, 2008, 10:24 AM
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.

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...)

Luke Redpath
Sep 1, 2008, 11:17 AM
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.

sujithkrishnan
Sep 2, 2008, 12:53 AM
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.

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

ploppy
Sep 2, 2008, 01:39 AM
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

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