Re: In-loop releasing of objects does not free memory?
Re: In-loop releasing of objects does not free memory?
- Subject: Re: In-loop releasing of objects does not free memory?
- From: John Terranova <email@hidden>
- Date: Tue, 12 Feb 2008 17:10:23 -0800
I guess I should have been more explicit and said to, first, verify
that retainCount > 1 with this type of NSLog(). Then, investigate why
retainCount > 1. If it is an autorelease, then "try wrapping the body
of the loop in an autorelease pool," if that is your chosen solution.
If there are no autoreleases (or not enough to account for the
retainCount), then you must "find" your missing auto/release to patch
the real leak.
On Feb 12, 2008, at 4:33 PM, John Stiles wrote:
I think a better way to explain this would be, "try wrapping the
body of the loop in an autorelease pool."
John Terranova wrote:
On Feb 12, 2008, at 2:10 PM, Arthur C. wrote:
NSArray * files; // contains a list of filenames
int c = [files count];
/*
for(i=0;i<[files count]; i++)
*/
for (i= 0; i < c; i++) // why do [files count] every iteration?
since files is immutable
{
NSString * dataFromFile = [[NSString alloc]
initWithContentsOfFile: [files objectAtIndex: i]];
< some processing >
NSLog(@"retainCount = %d means memory will%@be freed",
[dataFromFile retainCount], [dataFromFile retainCount] > 1 ? @" not
" : @" ");
[dataFromFile release];
}
If you see any "will not be freed" messages in the console, then
dataFromFile has been retained, but either not released or
autoreleased somewhere inside the loop. Just one autorelease,
instead of release will cause the memory to be freed when the
autorelease pool is drained (way up in the main loop, if you
haven't put one in place somewhere closer to this code).
john
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden