Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: In-loop releasing of objects does not free memory?




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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >In-loop releasing of objects does not free memory? (From: Arthur C. <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.