• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Garbage Collection in Objective-C++
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Garbage Collection in Objective-C++
      • From: Chris Hanson <email@hidden>
  • Prev by Date: Re: [[NSGarbageCollector defaultCollector] isEnabled] wrong?
  • Next by Date: weird drawing when resizing an outline view
  • Previous by thread: Re: - [UIImage draw9PartImage:inRect:] equivalent in Cocoa/Quartz?
  • Next by thread: Re: Garbage Collection in Objective-C++
  • Index(es):
    • Date
    • Thread