• 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: autorelease - when is retain count decremented
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: autorelease - when is retain count decremented


  • Subject: Re: autorelease - when is retain count decremented
  • From: j o a r <email@hidden>
  • Date: Tue, 14 Sep 2004 23:32:53 +0200

My advice is that you should avoid to think about memory and pointers when you work in Cocoa. Think objects, and references to objects.

NSString *aString = [NSString string];

Now you have a reference to a string object. The string object was created from a class factory method, and you are therefore not required to think about when or how to release it - as you don't retain it.

aString = [NSString string];

Now you have a reference to another string object. You no longer have a reference to the first string object, but from a memory management perspective it doesn't matter - as it was autoreleased there is no memory leak.

Autoreleased objects are released when the autorelease pool that have a reference to them is itself released. This *usually* happens at the end of the "event loop", ie. at the end of the chain of events that is triggered by for example a mouse click or a key press by the user.

Use Google and the mailing list archives help you find all the nice articles written on Cocoa memory management that are available on the web!

j o a r

On 2004-09-14, at 22.41, justin webster wrote:

just wondering what causes the retain count of an object to drop.
this is the situation I've been wondering about:

NSString *myString = [NSString stringWithFormat:@"testing"];
myString = anotherString;

when I point myString away from the 'testing' string, does the 'testing' memory get freed?
or should I release myString before pointing it to something else?


also, in this situation do I now have two pointers to the same piece of memory, and therefore a retain count of two?

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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

References: 
 >autorelease - when is retain count decremented (From: justin webster <email@hidden>)

  • Prev by Date: Re: autorelease - when is retain count decremented
  • Next by Date: Re: autorelease - when is retain count decremented
  • Previous by thread: Re: autorelease - when is retain count decremented
  • Next by thread: Re: autorelease - when is retain count decremented
  • Index(es):
    • Date
    • Thread