discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dealloc Notification


From: Gerrit Van Dyk
Subject: Dealloc Notification
Date: Tue, 13 Nov 2001 18:23:35 +0200

Hi all,

Is there a way of registering an object to get notified when another object
deallocs? I am busy rewriting some of the code in the Database Library and I
need to know when an object gets deallocated, so that I can remove the
unique instance for the object's primary key. I am experiencing quite a bit
of problems with the current Database Library because it uses the GC family
of classes in the extensions library.

If there is no such type of notification, how much of an overhead will the
following code add to the default dealloc in NSObject for objects that is
not registered for dealloc notification?.

#import <Foundation/NSHashTable.h>
#import <Foundation/NSNotification.h>

@implementation NSObject

static NSHashTable *_deallocNotify = NULL;

- (void)registerForDeallocNotification
{
   if (!_deallocNotify)
      _deallocNotify =
         NSCreateHashTableWithZone(NSNonOwnedPointerHashCallBacks,
                                   100,
                                   NSDefaultMallocZone());
   NSHashInsertIfAbsent(_deallocNotify, self);
}

- (void)unregisterForDeallocNotification
{
   if (_deallocNotify)
      NSHashRemove(_deallocNotify, self);
}

- (void)dealloc
{
   if (_deallocNotify)
      if (NSHashGet(_deallocNotify, self))
      {
         [[NSNotificationCenter defaultCenter]
            postNotificationName:NSObjectDidDeallocNotification
            object:self];
         NSHashRemove(_deallocNotify, self);
      }
   [super dealloc];
}

@end

Obviously this needs to built into NSObject, so NSObject's code will need to
change, or alternatively this should be a class that gets posedAs a NSObject
when the DB Library gets accessed, this will need more work but should be
possible.
The overhead will be the test to see if the hashtable exist and then to look
up the object in the hashtable.
I don't experience much of an overhead on my side, but I am running on a
PIII 1000Mhz with 512MB Memory.

I do remember seeing something similar when I debugged an Openstep EOF
application once.

This will work for any object that is interested in knowing when another
object gets deallocated. You simply register the object for
deallocNotification and then subscribe for the
NSObjectDidDeallocNotification.

Regards
Gerrit van Dyk
 
-------------------------------------------------------------------------
This e-mail is intended only for the use of the individual or entity named
above and may contain information that is confidential and privileged,
proprietary to the company and protected by law. If you are not the intended
recipient, you are hereby notified that any dissemination, distribution or
copying of this e-mail is strictly prohibited. Opinions, conclusions and
other information in this message that do not relate to the official
business of our company shall be understood as neither given nor endorsed by
it.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]