Is it possible to override `release` for debugging?

Sometimes I need to find out if an object will actually be released. Of course, I could use the Tools, but it takes a lot of time, and I have to search for millions of objects, so I used this:

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

But the problem is what is safe to do ? Can I get any problems when I override -(void)release . Also, is it really void ? And what if I create an application for distribution, but in case of an accident you leave it? Or is it just safe? Thanks

+7
debugging memory-leaks objective-c cocoa-touch release
source share
2 answers

This is good, but please limit it to debugging only.


It is 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, -release messages sent to "free bridge containers" (for example, NSCFArray, etc.) will be missed, as they also redefined -release for forwarding to CFRelease .

+19
source share

The release message only decreases the instance reference count.

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

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

Use it wisely.

+5
source share

All Articles