Re: [Newbie Q] Memory Management in Cocoa
Re: [Newbie Q] Memory Management in Cocoa
- Subject: Re: [Newbie Q] Memory Management in Cocoa
- From: Andy Lee <email@hidden>
- Date: Tue, 5 Nov 2002 22:40:18 -0500
At 3:16 PM -0800 11/5/02, Douglas Davidson wrote:
On Tuesday, November 5, 2002, at 03:12 PM, Jonathan E. Jackel wrote:
I've always liked the dog-leash metaphor. Imagine a dog. If you want to
walk the dog you put a leash on his collar. But the dog is very popular.
Any number of people can "retain" the dog by adding any number of leashes.
When each person is tired of walking the dog, he/she releases the leash.
When the last person let's go of the last leash, the dog is "freed".
That's a striking metaphor, but I think it really doesn't go anywhere...
I first saw it in Hillegass. I found it easy to visualize, but
something about it didn't feel right. I think it was pity for the
dog.
The notion of a leash-holder isn't exactly right, as there isn't a
hard connection between the retainer of an object and the object --
it's purely a matter of programming practice. You could change the
metaphor so the dog is leashed to a fence, and anybody can add a
leash, and anybody can remove a leash. As a matter of *convention*
and good citizenship, we are only responsible for the leashes we add,
and we don't know or care how many leashes there are altogether. An
autorelease pool could be a special part of the fence from which all
leashes are removed periodically by a special anonymous person whose
job that is.
To me there's another problem -- the "freed" dog isn't quite like
freed memory. I think a balloon metaphor would be more fitting,
since a balloon is essentially a malloc of a specifiable quantity of
air. So then instead of leashes on a dog, you could have strings on
a balloon. When the last string is released -- whoosh, the air is
dealloced and once more available for someone else to make a balloon
with.
But there's still a problem with any of these physical metaphors,
because although they illustrate how memory leaks can occur (and to
that extent are useful), they don't illustrate what happens when you
send too many -release messages to an object -- and why that
sometimes causes immediate crashes, sometimes delayed crashes, and
sometimes "message not understood" exceptions.
I realize I'm analyzing this way more than necessary. Hey, I once
seriously wondered how Mama Bear's porridge could be too cold if it
was between Papa Bear's too-hot porridge and Baby Bear's just-right
porridge. I think there are plausible explanations, but I'll spare
you the details...
--Andy
_______________________________________________
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.