On Mar 11, 2009, at 10:03 AM, Robert Mullen wrote:
As I have waded into the code a little more I see that this entire struct is allocated using calloc() in an init and freed with free(). How does that work with GC? I have tried __strong and some CFRetain and CFRelease to no avail but want to try rearranging things a little bit there. I think the struct itself may be the problem but would like some corroboration.

Yes, the struct itself is a problem. When the garbage collector looks for pointers, it doesn't look inside ordinary malloc-allocated memory. If the only pointer to your object is inside this malloc'ed struct, the collector won't see it and may throw your object away.

You have three options:

1. Turn the struct into an Objective-C object. You can mark all of the ivars @public to make it more struct-like, instead of adding accessor methods everywhere. Of course, you now need to make sure the pointers to the former struct are GC-compatible (for example, not inside other malloc'ed structs).

2. Allocate the struct with NSAllocateCollectable(NSScannedOption| NSCollectorDisabledOption) AND mark the object fields in the struct with __strong. NSScannedOption tells the collector to look inside the struct for other GC pointers. NSCollectorDisabledOption tells the collector that you'll call free() yourself. __strong tells the compiler to use the GC write barriers when assigning to the struct's fields.

3. CFRetain() pointers before storing them in the struct, and CFRelease () them afterwards. This can be easy if the struct's fields are wrapped in accessor functions, but otherwise may be a large code change. Beware of uncollectable cycles.


--
Greg Parker     gpar...@apple.com     Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to