feeling retained?

This is a quick tip for figuring out why an object is not being released when you expect it to:

For the object in question, override retain and release to log their activity. Don’t forget that alloc is always called first, so if you see an equal number of retain and release logs, this means your object still has a retain count of 1 (from the alloc). If you want to know who is retaining you, just add a breakpoint in your retain method, and check the stack trace each time it’s called.

Sample code:

@implementation MyObject

- (id)retain {
  NSLog(@"%s on %p", __FUNCTION__, self);  // Add breakpoint here to see who retained us.
  return [super retain];
}

- (void)release {
  NSLog(@"%s on %p", __FUNCTION__, self);
  [super release];
}

@end

One Trackback

  1. […] a previous post, I mentioned a trick that I sometimes use to debug memory issues where a class I own is either […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*