Re: Garbage collector vs variable lifetime
Re: Garbage collector vs variable lifetime
- Subject: Re: Garbage collector vs variable lifetime
- From: Antonio Nunes <email@hidden>
- Date: Sat, 7 Jun 2008 09:07:44 +0200
On 7 Jun 2008, at 06:16, Ken Thomases wrote:
On Jun 6, 2008, at 10:48 PM, Antonio Nunes wrote:
Why not explicitly turn off collection for the data pointer:
NSData* data = <get it from somewhere>;
[[NSGarbageCollector defaultCollector]
disableCollectorForPointer:data];
const unsigned char* bytes = [data bytes];
NSUInteger count = [data length];
for (NSUInteger i = 0; i < count; i++)
something = bytes [i];
[[NSGarbageCollector defaultCollector]
enableCollectorForPointer:data];
It's slightly longer, but straightforward and clear about it's
purpose, and should be future proof.
And... we're back to retain/release. [...]
The issue is, how can one know when this technique is necessary?
By proper documentation. As Bill mentioned earlier, the documentation
needs to mention this behaviour. Just as retain/release, GC has its
own caveats, it's own moments where one has to take special care. This
appears to be one of them. If I read Bill's explanation correctly then
once the documentation gets updated and distributed anyone can know
when this technique is necessary (as long as they know what they're
doing, which goes for anything in programming).
On 7 Jun 2008, at 07:03, Bill Bumgarner wrote:
The problem here is that there is no way for the compiler to know
that the char* bytes is somehow connected to the lifespan of the
NSData*. Nor is there any way for the collector to know that the
char* bytes is somehow an implied root of the NSData* object. [etc...]
This is an edge case. It is uncommon and the kind of edge case
that only exists when you dip below Objective-C and into the realm
of C.
Although I maybe did not make it that clear, I actually meant my
suggestion also as a question. I'm surprised no-one else suggested to
temporarily turn of garbage collection for this pointer. I'm curious
as to why Bill suggested his solution rather than directly instructing
the garbage collector, since that would seem to me the more obvious
solution. Is Bill's solution preferable in any way? (After all, I'd
like to improve to the level where I know what I'm doing ;-)
-António
-----------------------------------------
Perfume is the forgiveness
that the trampled flower casts
upon the heel that crushes it.
-----------------------------------------
_______________________________________________
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