Re: Converting from Carbon Event Manager to NSTimer
Re: Converting from Carbon Event Manager to NSTimer
- Subject: Re: Converting from Carbon Event Manager to NSTimer
- From: Graham Cox <email@hidden>
- Date: Fri, 26 Sep 2008 20:01:40 +1000
On 25 Sep 2008, at 9:18 am, Dan Birns wrote:
I must say, after 20 years of C malloc(), I find retain/release
mysterious. It's probably no better or worse than malloc/free, but
confusing to me. It seems to me that it has exactly the same
advantages/disadvantages, but it's just wildly different. Who does
the free()? Who does the retain/release? Pretty much the same
problem...
retain/release is not the same as malloc/free, and I don't think it
will help you to consider them somehow equivalent. FWIW, malloc/free
still work normally in Obj-C, retain/release is in addition.
[someObject alloc] probably calls malloc() internally.
Do read this:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
When I first encountered retain/release, it was through Hillegasse's
book, and his analogy stood me in very good stead when getting my head
around retain/release. To paraphrase Hillegasse:
An object is like a dog. When you retain it, you attach a leash to it,
so it stays with you. When you release it, you unclip your leash. If
another person also retains the dog, they attach their own leash to
it. As long as at least one person has attached a leash to the dog,
the dog is under control and sticks around. When everyone unclips
their leashes, the dog goes free - it still exists, but now not on
anyone's leash, so it runs off and gets flattened by a bus (thus
finally "deallocating" it). Autorelease is nothing magic either, it's
just an automatic leash which unclips itself when the current event
completes.
In code, the "dog" is guaranteed to be flattened by a bus when no-one
has a leash on it (apologies to dog-lovers), at which point [dog
dealloc] is called (which probably internally calls free()).
hth,
Graham
_______________________________________________
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