• 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: NSObject Foundation tool memory issue?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSObject Foundation tool memory issue?


  • Subject: Re: NSObject Foundation tool memory issue?
  • From: Scott Ellsworth <email@hidden>
  • Date: Thu, 08 Dec 2005 15:31:51 -0800


On Dec 8, 2005, at 3:05 PM, Lee Cullens wrote:

Thanks to your suggestions I found what is causing the problem. I create and autorelease an object within the scope of the base pool. Then because I do so several times in one test loop iteration, the object's common print function (with a local pool) releases the object

That is the problem.

Remember the rules of Cocoa memory management:

<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/ index.htm>

1. If you alloc'ed it, you (auto) release it.
2. If you copy'ed it, you (auto) release it.
3. If you retained it, you (auto) release it.

and the corollaries:

4. If you want to keep it in memory, retain and (auto) release it.
5. Retain even autoreleased objects if you want them to live past the next run of the event loop.
6. Collections retain their values, so keeping a retain on a collection 'counts' in the point 5 sense.

I find release better than autorelease, as it causes the object to vanish right away if nobody else is retaining it, and gives you a bus error immediately, rather than at the next event loop.

On the other hand, if you are writing a method named anything other than alloc or copy, and you return an object value, you pretty much have to autorelease it. The programmer will look at your name, see that it is not 'alloc' or 'copy', and figure they do not need to release the result.

Point 5 often surprises people who pull something out of an NSArray, then release the array. At that point, if you did not retain, your object may have just been released underneath you.

Anyway, a specific question. If I create-init-autorelease an object multiple times using the same variable, do I increase my memory footprint with the resulting unreferenced objects? That is:

Yes - all three stay around until the autorelease pool is emptied.

<rant>
I hate tracking down this category of problems. I can do it, but it is frustrating. Times that a GC system looks real attractive.
</rant>

Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: NSObject Foundation tool memory issue?
      • From: Lee Cullens <email@hidden>
References: 
 >NSObject Foundation tool memory issue? (From: Lee Cullens <email@hidden>)
 >Re: NSObject Foundation tool memory issue? (From: Niko Matsakis <email@hidden>)
 >Re: NSObject Foundation tool memory issue? (From: Lee Cullens <email@hidden>)
 >Re: NSObject Foundation tool memory issue? (From: Niko Matsakis <email@hidden>)
 >Re: NSObject Foundation tool memory issue? (From: Nick Zitzmann <email@hidden>)
 >Re: NSObject Foundation tool memory issue? (From: Lee Cullens <email@hidden>)

  • Prev by Date: Re: NSObject Foundation tool memory issue?
  • Next by Date: WebDataSource, Cocoa Plug-in puzzlement
  • Previous by thread: Re: NSObject Foundation tool memory issue?
  • Next by thread: Re: NSObject Foundation tool memory issue?
  • Index(es):
    • Date
    • Thread