Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A coding pattern that does not work under Garbage Collection



On Nov 9, 2007, at 10:39 AM, Rick Hoge wrote:

I've been testing some of our older libraries under GC, and have found one coding pattern (that may well have been bad practice anyway) that really does not work well under GC.

I used to use convenience constructors for NSMutableData objects as a kind of lazy replacement for malloc.

That is, an assignment like this:

float *myPointer = [[NSMutableData dataWithLength:size*sizeof (float)] mutableBytes]; // Memory freed at end of event loop

[...]

It seems like this (not surprisingly) doesn't work reliably under GC, as the collector obviously has no way of knowing that you are later looking at the memory originally addressed by myPointer. It keeps track of objects and not pointers.

You kept a (default, no strong or weak modifier) pointer to data inside the object, but no one kept a pointer to the object, so it was collected.


As there are probably better ways of approaching this kind of situation, I'd be interested in any suggestions or comments. The way I was doing it before was probably just bad...

You can allocate GC'd memory with NSAllocateCollectable.

Jim
_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >A coding pattern that does not work under Garbage Collection (From: Rick Hoge <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.