Re: Memory allocation questions
Re: Memory allocation questions
- Subject: Re: Memory allocation questions
- From: Jeff LaMarche <email@hidden>
- Date: Sat, 27 Jul 2002 23:18:03 -0700
On Saturday, July 27, 2002, at 09:52 PM, Terry Simons wrote:
>
I'm confused about a lot of the Apple runtime stuff, but I'll tackle
>
the confusion one subject at a time. ;)
>
>
The ObjectiveC language all by itself doesn't seem too complicated, but
>
I'm confused when it comes to the memory management aspects that are
>
part of Apple's runtime.
>
>
It seems like this is a very common problem for people new to Cocoa,
>
but I couldn't find the answers to my specific questions in the lists.
>
What *exactly* does autorelease do differently than release?
Don't feel too bad about not getting it immediately. It's a hard concept
for people to grasp, whether they are coming from Java where you don't
worry about memory allocation too much, or they are coming from C/C++
where every byte has to be accounted for.
Autorelease basically tells the runtime that you want the object
released, but that you want to give other objects a chance to grab and
retain it. What it does is wait until the end of the event loop and THEN
reduces the retain count and (if appropriate) releases the object. That
means that at very least, the method from which your method that returns
an autoreleased object is called will have access to it. If they don't
retain it, it will get deallocated, but at least they have the option.
If you use alloc then you have created a non-released object and are
responsible for releasing or autoreleasing it at some point when you are
done with it. However, if you do not actually call the alloc method, but
rather are handed an object by another method (either classs or
instance), then it is generally safe to assume that it is autoreleased
and that you should retain it if you want to keep it past the end of the
current method.
You need to balance your calls. If you call alloc, then you must call
release or autorelease. Same is true for retain.
Hope this helps. There's a good article at www.stepwise.com on Cocoa
memory management which should clear up the confusion for you.
- Jeff
_______________________________________________
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.