Re: More memory management questions
Re: More memory management questions
- Subject: Re: More memory management questions
- From: Jonathan Jackel <email@hidden>
- Date: Sun, 03 Mar 2002 14:33:08 -0500
on 3/1/02 8:37 PM, David Newberry at email@hidden wrote:
>
The summary of "Object Ownership and Disposal" says:
>
"* If you directly allocate, copy, or retain an object, you are
>
responsible for releasing the newly created object with release or
>
autorelease. Any other time you receive an object, you are not responsible
>
for releasing it."
The very best, easiest to understand description of retain and release that
I have seen is at:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
If that's not enough, check out:
http://www.stepwise.com/Articles/Technical/HoldMe.html
Until I read them yesterday, I had no clue. Now I think I know how to
answer your question.
>
My question here is, if I do something like this:
>
NSString *string = [NSMutableString stringWithString:@"temp"];
>
Have I "allocated" this object? If not, should it be treated as a received
>
object (below)?
Basically, yes. Objects that are created with "convenience constructors"
are autoreleased. Convenience constructors are anything other than
alloc/init and copy. The stringWith... methods create autoreleased objects.
>
>
"* A received object is normally guaranteed to remain valid within the
>
method it was received in (exceptions include multithreaded applications
>
and some Distributed Objects situations). That method may also safely
>
return the object to its invoker."
>
>
This sounds like (as far as my understanding goes), you should treat
>
received objects as though they were sent an autorelease message before
>
being returned. Is this a reasonable assumption, at least for
>
understanding the object's behavior?
Close enough. You should not send a received object a release message
because that object is "owned" by the sender. The sender is responsible for
releasing (or autoreleasing) the object. On the other hand, if your method
needs to make sure the received object sticks around after your method
finishes (e.g., the object is the value of an instance variable), retain the
received object.
>
>
"* If you need to store a received object in an instance variable, you
>
must retain or copy it."
>
>
This also sounds to me like it was sent an autorelease message.
See above.
>
"* Use retain and autorelease when needed to prevent an object from
>
being invalidated as a normal side-effect of a message."
>
>
My question here is: If you have an allocated object that hasn't received
>
any retain or [auto]release messages, what will it do? Stick around
>
forever? or be removed with the next NSAutoreleasePool call?
If you create the object with alloc/init or copy, the object will stick
around until you explicitly send it a release or explicitly add it to an
autorelease pool which is then released (or quit the app).
If you create the object with a convenience constructor, it is automatically
added to the autorelease pool and is autoreleased shortly after the code
block finishes executing.
Jonathan Jackel
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.