Re: NSAllocateCollectable() questions
Re: NSAllocateCollectable() questions
- Subject: Re: NSAllocateCollectable() questions
- From: "Clark Cox" <email@hidden>
- Date: Wed, 16 Apr 2008 10:48:06 -0700
On Wed, Apr 16, 2008 at 9:26 AM, Michael Ash <email@hidden> wrote:
> On Wed, Apr 16, 2008 at 12:04 PM, Clark Cox <email@hidden> wrote:
> > The purpose of the write barrier is to tell the collector to keep
> > something alive; it has nothing to do with when to collect it.
>
> Right, I get that now.
>
> [snip]
>
> > Garbage Collection:
> > The housekeeping is taken care of, I don't have to worry about it.
> > However, I cannot know exactly when my object will be destroyed (it
> > might be right away, it might be in a couple of seconds).
>
> I don't see the relevance of this. The Cocoa GC explicitly only works
> properly if you play by the rules it sets out. For example, if you
> store a pointer in unscanned memory then you're playing with fire and
> the object may well have been destroyed by the next time you try to
> use it.
But, in normal Cocoa patterns, after doing some relatively trivial
replacements (i.e. use NSAllocateCollectable instead of malloc, etc.),
it usually takes effort to store pointers in unscanned memory.
> Skipping write barriers likewise.
Again, under most circumstances, it usually takes effort to skip the
write barriers, as they are added automatically by the compiler.
> I had thought that using
> write barriers for clearing memory was part of the required rules, but
> now it appears that it is not. But regardless, Cocoa GC only takes
> care of its housekeeping when you take care of yours.
Indeed. GC doesn't allow the developer to abdicate all memory
management responsibility; however, the responsibilities that one
still has are much smaller in scope and severity (i.e. "Don't store a
GC-managed pointer into non-scanned memory") , and the exceptions to
the rules are few and far between. In the past year, I've had to make
a special allowance for the garbage collector only once that I can
recall.
Generally, the only difference between my non-GC and my GC code is
that the GC code lacks retain, release and autorelease calls as well
as dealloc implementations. At this point, if I could do away with
writing pre-GC Cocoa code, I would do so in a heartbeat.
--
Clark S. Cox III
email@hidden
_______________________________________________
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