Re: Garbage Collection Docs Puzzle
Re: Garbage Collection Docs Puzzle
- Subject: Re: Garbage Collection Docs Puzzle
- From: Dave Keck <email@hidden>
- Date: Mon, 25 Jan 2010 13:06:55 -1000
> What's with the retain and release? I understand the issue of keeping myData alive until you're finished with the interior pointer, but why [myData release]? Is the release enough to keep myData alive even thought its dispatch is short circuited under GC? And the retain is there to balance the release in case someone executes under managed memory? Or did somebody mean CFRetain and CFRelease?
-retain and -release are indeed enough to prevent myData from being
collected (and therefore the interior pointer from going stale). The
issue being worked-around is that of the compiler; during
optimization, if you stop referencing myData, the compiler may decide
to reuse the stack slot reserved for it. Once that reference
disappears (assuming it was the last rooted, strong reference) myData
will be collected. (Note that the short-circuiting issue is
irrelevant, because it doesn't occur at compile-time - it happens in
objc_msgsend.)
Under managed-memory, the -retain/-release will ensure that the data
object is kept alive until you're threw with the interior pointer, in
the case that -getMyData didn't return a -retain/-autoreleased object.
> It seems that [myData self] or [myData class] or some other harmless (return value discarded) non-sequitor might be less confusing. It would point out that this is an issue that is being worked around and not an ordinary part of memory management.
Indeed, you can choose to send myData any message you like - perhaps
an appropriately-named macro would be more fitting. And of course if
you're writing GC-only code then the -retain is unnecessary - for that
reason sending myData -self at the end of the method would make a lot
more sense.
_______________________________________________
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