Re: Calling -retain inside -dealloc
Re: Calling -retain inside -dealloc
- Subject: Re: Calling -retain inside -dealloc
- From: Andy Lee <email@hidden>
- Date: Mon, 27 Jun 2005 08:53:10 -0400
On Jun 27, 2005, at 5:45 AM, mmalcolm crawford wrote:
On Jun 27, 2005, at 12:15 AM, Andrew Garber wrote:
The problem isn't that my class is suffering performance-wise.
This is
just an experiment to see how much of a performance increase can be
gained in this case by using an alternative to NSNotifications.
I'm afraid this still doesn't explain what the relevance is of
memory management, and in what respects this is an alternative to
notifications.
If I understand correctly, Andrew's case is similar to notifications
in that it involves a global collection of objects that operates
behind the scenes. Such collections are used all the time. One use
is to serve as a query cache. Another use is for debugging -- for
example, if you want to be able to see all instances of a given
class. NSApp's window list is a global collection.
Andrew's use of a global collection is to "notify" objects of some
event, kind of like what NSNotificationCenter does. So would one not
simply use NSNotifications?
* Maybe, as in Andrew's case, you want to experiment and see if you
can get a performance gain.
* Maybe you want more control over the posting of the notification.
For example, you might want to control the order in which objects
receive the notification, or you might want to check whether one of
the objects fails, and if so, stop propagating notifications.
* Maybe, for debugging purposes, you want to see all the objects that
can receive your notification at any given time.
* Maybe you want to guarantee that only certain kinds of objects can
register for the notification.
The relevance to memory management is the same as for other similar
global collections. We want the collection to have a weak reference
to its objects, such that an object will get removed from the
collection when the *rest* of the application has finished with it.
The convention for NSNotifications is that the "global collection" (I
don't know what data structure is actually used) does not retain its
objects. Thus the registered objects follow the normal retain-
release lifecycle in all ways -- but with the caveat that they must
unregister themselves in their -dealloc method.
This pattern was among the suggestions made to Andrew: i.e., use a
non-retaining collection for the global collection -- either a
properly configured CFMutableArray, or a regular NSMutableArray that
contains NSValue wrappers around object references instead of the
objects themselves.
--Andy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden