Garbage Collection in Objective-C++
Garbage Collection in Objective-C++
- Subject: Garbage Collection in Objective-C++
- From: David Elliott <email@hidden>
- Date: Tue, 5 Feb 2008 21:53:43 -0500
Hello,
I am having some trouble with GC in my Objective-C++ code.
Specifically with respect to global pointers to Objective-C objects.
The original code looked like this:
wxObjcAutoRefFromAlloc<void*> s_foo = [[SomeClass alloc] init];
The idea is that wxObjcAutoRefFromAlloc constructs with an already-
retained object (e.g. one from alloc) and releases on destruction.
Copy construction results in a retain so that's not an issue. Ignore
that class for now though as the implementation of it is horrible, so
don't go looking it up.
So, moving along, I changed that from void* to struct objc_object* and
indeed I now see in the assembler the objc_assign_strongCast calls in
the C++ constructor so the write-barriers are clearly being
generated. Unfortunately, the object that should be considered
referenced is not considered referenced and it gets finalized almost
immediately.
What I believe is happening is that objc_assign_strongCast has no
effect on global memory, only heap-allocated memory. Adding an
explicit call to objc_assign_global works. I suppose I'll have to
write an alternate pointer-holder class specifically for globals if I
am to work with the GC library as is. Have I gotten anything wrong
here?
Now, there's one more problem. Consider this:
id foo = [[SomeClass alloc] init];
The compiler ought to generate a write-barrier there but doesn't at
all. You can manually do it like this:
id foo = objc_assign_global([[SomeClass alloc] init], &foo);
Obviously the object then never gets finalized whereas in the original
code it would be released and thus most likely deallocated during
static destruction time. That isn't really a problem for my code and
actually is beneficial since it's rather useless to clear the heap
when the process is about to exit.
It all boils down to these questions: am I correct in thinking that
objc_assign_strongCast cannot be applied to C++ instance variables
when they are located in global memory? Does that not make it
basically impossible to write a single generic C++ Objective-C-pointer
wrapping class which is admittedly more or less useless in GC mode but
quite handy in RR mode? Is my only option to have a special class
used when the C++ object is to be constructed in global memory which
manually invokes the global assign function?
If I do decide to write this class, is it then appropriate to assign
the value nil to the instance variable such that the GC can consider
the object finalizable? Will it actually be finalized before the
process ends? Not that it matters in this specific case, but I'm
curious.
-Dave
_______________________________________________
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