Re: Garbage Collection in Objective-C++
Re: Garbage Collection in Objective-C++
- Subject: Re: Garbage Collection in Objective-C++
- From: Chris Hanson <email@hidden>
- Date: Tue, 5 Feb 2008 20:50:10 -0800
On Feb 5, 2008, at 8:39 PM, David Elliott wrote:
wxObjcAutoRefFromAlloc<void*> s_foo = [[[SomeClass alloc] init]
autorelease];
There is no NSAutoreleasePool during C++ static initialization time
so this is unworkable.
In this case, in fact, [[SomeClass alloc] init] is also unworkable
because you don't know what side-effects +[SomeClass load], +
[SomeClass initialize] or -[SomeClass init] -- or any similar
superclass methods -- will have.
You can't mix C++ and Objective-C this way. Sorry.
In other words, assigning an object reference *to* one of these
should cause a retain.
I agree with you that your idea more closely follows Objective-C
semantics but it goes completely against C++ pointer-holder semantics.
You're dealing with Objective-C code, so you need to follow its
conventions. One of these is that taking ownership generally either
retains or copies. Another is that you can't run code outside an
autorelease pool (when running on the non-GC runtime).
A much better class to look at in wx is the wxCFRef<refType> that I
wrote years later (i.e. a few months ago instead of a few years
ago). Like the standard library auto_ptr or TR1 shared_ptr it has
an explicit constructor from a pointer which assumes it is taking
ownership of the pointer.
"Taking ownership" of a Cocoa object means retaining it.
I understand why you're following the path you're on, but I really
think you're ultimately making things harder for yourself by doing
so. When working with Cocoa, you really do need to follow the Cocoa
patterns.
-- Chris
_______________________________________________
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