• 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
Re: Guidelines for Cocoa frameworks supporting garbage collection?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guidelines for Cocoa frameworks supporting garbage collection?


  • Subject: Re: Guidelines for Cocoa frameworks supporting garbage collection?
  • From: Chris Hanson <email@hidden>
  • Date: Sat, 05 Jul 2008 14:56:46 -0700

On Jul 5, 2008, at 2:23 PM, Bill Cheeseman wrote:

So, I guess my frameworks should declare ...

  __strong CGEventRef myEvent;
  __strong AXUIElementRef myUIElement;

... if I am going to advertise the frameworks as supporting both
retain/release and garbage collection. Is that right?

That's correct.

Note that object lifetimes of even CF objects will be different under GC: CF objects will behave like Objective-C objects in that they will be collected when they are no longer referenced, rather than when their reference count goes to 0.

Under GC, CFRetain/CFRelease still manipulate an object's reference count, and are therefore *not* isomorphic to -retain/-release as they are under non-GC. So you need to be careful with how you use toll- free bridged objects. For example, this pseudocode is wrong under GC:

  - (NSURL *)someURL {
      NSURL *URL = (NSURL *)CFURLCreate...(...);
      return [URL autorelease];
  }

It will leak, because the runtime will "eat" the -autorelease message when running under GC. The correct way to write this is:

  - (NSURL *)someURL {
      NSURL *URL = NSMakeCollectable(CFURLCreate...(...));
      return [URL autorelease];
  }

When running under GC, the call to NSMakeCollectable will balance the initial retain count of of 1 for the CF object. This ensures that the CF object will be collected when there are no more references to it. When running under non-GC, the call to NSMakeCollectable does nothing, so the -autorelease balances the initial retain count of 1.

I've already included
-finalize methods to complement the -dealloc methods. I don't think I need
to do anything else, but I'm far from sure.

I haven't looked at your framework, but I do want to point out that - finalize methods should be relatively rare; generally speaking, you shouldn't need a -finalize everywhere you need a -dealloc.


Often people will write a -finalize method because they need to ensure some scarce resource (like a file handle) is released when their object is no longer referenced. It's good to have this as a safety net, but it's also good to have some form of -close or -invalidate method that will clear out scarce resources at a deterministic point.

NSFileHandle is a good example of this even outside of GC: An NSFileHandle *can* close its file descriptor on -dealloc, but generally you'll want to send an instance -closeFile as soon as you're done with it so you don't risk running out of file descriptors if the lifetime of the NSFileHandle instance has been extended.

  -- 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


  • Follow-Ups:
    • Re: Guidelines for Cocoa frameworks supporting garbage collection?
      • From: "Sean McBride" <email@hidden>
    • Re: Guidelines for Cocoa frameworks supporting garbage collection?
      • From: Bill Cheeseman <email@hidden>
References: 
 >Guidelines for Cocoa frameworks supporting garbage collection? (From: Bill Cheeseman <email@hidden>)

  • Prev by Date: Method Sees Populated Array As Empty
  • Next by Date: Re: Method Sees Populated Array As Empty
  • Previous by thread: Guidelines for Cocoa frameworks supporting garbage collection?
  • Next by thread: Re: Guidelines for Cocoa frameworks supporting garbage collection?
  • Index(es):
    • Date
    • Thread