On Sep 25, 2010, at 3:31 PM, Richard Somers <[email protected]> wrote:

> I need a class, in which an instance of the class, can access all other 
> instances of the class.
> 
> In the following code, instances of the class are stored in a static mutable 
> array. To make the code work, the instance must released after adding it to 
> the array and retained before removing it from the array. Otherwise dealloc 
> will never be called, and instances will never be removed from the array.

Usually the better approach is to pick a time at which it makes sense to remove 
the object from the array. For example, if you were writing a socket wrapper 
class and needed to keep a global array of all socket objects, you might add 
the object to the array in -init, and remove it in -close.

If you can't find a good method to use, I'd advise adding one specifically for 
this purpose. We have a few of these, and typically call them -invalidate. As 
long as everyone plays by the memory management rules, no overrides of -retain 
or -release are required, and calling -invalidate breaks the retain cycle that 
would otherwise keep the object alive.

I would not recommend playing fancy memory management tricks in -init or 
-dealloc. You're likely to make a mistake (memory management is a noodly enough 
topic already), and they will make your code more confusing in a garbage 
collected environment.

--Kyle Sluder_______________________________________________

Cocoa-dev mailing list ([email protected])

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