views:

98

answers:

2

Sometimes I need to find out if an object will really be released. I could use Instruments of course, but that takes much time, and I have to search into millions of objects, so I used to do this:

-(void)release {
    NSLog(@"I'm released");
    [super release];
}

But the problem is: is this safe to do? Can I get any problems when I override -(void)release. Also, is it really void? And what if I build my application for distribution, but per accident leave it there? Or is it just safe? Thanks

+6  A: 

It's fine, but please restrict it for debugging only.


It's not void, but oneway void.

-(oneway void)release {
    NSLog(@"I'm released"); // <-- remeber the @.
    [super release];
}

Note that if you only override this for NSObject, then the -release messages sent to "toll-free bridged containers" (e.g. NSCFArray, etc.) will be missed since they've also overridden -release to forward to CFRelease.

KennyTM
+1  A: 

The release message only decrements the reference count of the instance.

If you want to know if the instance has been released, then the best is to override the dealloc message:

- (void)dealloc {
    NSLog(@"I am deallocated");
    [super dealloc];
}

Use it wisely.

Laurent Etiemble