• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: In-loop releasing of objects does not free memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • RE: In-loop releasing of objects does not free memory?
      • From: Arthur C. <email@hidden>
    • Re: In-loop releasing of objects does not free memory?
      • From: John Terranova <email@hidden>
References: 
 >In-loop releasing of objects does not free memory? (From: Arthur C. <email@hidden>)
 >Re: In-loop releasing of objects does not free memory? (From: John Terranova <email@hidden>)

  • Prev by Date: Re: layoutManager:didCompleteLayoutForTextContainer:atEnd: returns atEnd as NO dodgily for me (but works in TextEdit...)
  • Next by Date: NSURLConnection details
  • Previous by thread: Re: In-loop releasing of objects does not free memory?
  • Next by thread: Re: In-loop releasing of objects does not free memory?
  • Index(es):
    • Date
    • Thread