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 Stiles <email@hidden>
- Date: Tue, 12 Feb 2008 16:33:19 -0800
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