Hi all,

On a number of occasions I've found myself in a situation where I have a data structure (often a complex one) that is implemented in C, but for which I want an ObjC wrapper. As an example, let's consider a tree of nodes.

Now, obviously it would be good to provide NSEnumerator subclasses to iterate over the data structure. But -nextObject returns an ObjC object, so that means writing a node wrapper class too.

Unfortunately if you do that naïvely, iterating over the data structure will create the same number of temporary objects as the structure has nodes.

When running without GC, there is a simple "fix" for this, which is to re-use the existing object unless it has been -retained by something else. i.e. only create objects if someone is storing references to them. This way, only a single wrapper object is needed in 99.9% of cases. Obviously this is not perfect; for instance, if you wanted to iterate and remember one of the objects for use in a subsequent iteration, you must retain the reference (even if you immediately autorelease it), to stop the enumerator from re-using the object. But it works well enough.

Under GC, these kinds of tricks are no longer possible because there is no -retain call any more, so no way to tell if it is safe to re- initialise and return the same wrapper object.

Aside from re-implementing the data structure itself in ObjC and making all of its nodes objects, which might not be possible in some cases, has anyone thought of an efficient solution to this problem when running under GC?

(I should say that I'm not actively using GC myself in any production code. I'm just curious to know if anyone has solved this for the GC case.)

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to