3

Is there a way to watch the changes of the retain count of an object while debugging in Xcode?

jscs
  • 62,161
  • 12
  • 145
  • 186
Tuyen Nguyen
  • 4,159
  • 7
  • 45
  • 76
  • 3
    Why do you want to do this? `retainCount` doesn't mean what you think it means. – Jonathan Grynspan Mar 15 '12 at 20:48
  • Thanks for everyone answers. (Please let me know the reason why my question is bad before down voting it.) – Tuyen Nguyen Mar 15 '12 at 22:17
  • 1
    You should [never ever](http://whentouseretaincount.com) touch `retainCount`. For a more detailed explaination look here: http://stackoverflow.com/questions/4636146/when-to-use-retaincount – Erik Aigner Mar 15 '12 at 20:49
  • 1
    The question is perfectly fine. Not sure why anyone would down vote. – bbum Mar 16 '12 at 19:44

1 Answers1

0

Instruments will give you the most useful form of this information. Use that first.

If you must, you can override the relevant methods to do some logging:

- (oneway void) release {
   DLog(@"%p release", self);
   return [super release];
}

- (id) retain {
    DLog(@"%p retain", self);
    return [super retain];
}

- (id) autorelease {
    DLog(@"%p autorelease", self);
    return [self autorelease];
}

This may not work under ARC, I'm not sure.

ALSO, as others have pointed out, you shouldn't be caring about the absolute retain count of your objects. You should only worry about what you've claimed ownership of and therefore need to release. See: Calling -retainCount Considered Harmful and When to use -retainCount?

Community
  • 1
  • 1
jscs
  • 62,161
  • 12
  • 145
  • 186
  • 1
    There is never a **must** for `-retainCount` it doesn't give you any relevant information. – Erik Aigner Mar 15 '12 at 21:10
  • @Erik: that's not absolutely true, and the proof is that it's included in Instruments, as I noted. – jscs Mar 15 '12 at 21:22
  • 1
    What is included in Instruments is much less than the absolute retainCount and much more about the backtraces of all uses of retain/release/autorelease. Only in that context is the retainCount useful, but then is a very minor detail. – bbum Mar 15 '12 at 21:39
  • 1
    @bbum: Certainly; I've said on many occasions (including here) that the absolute retain count is useless. I'm not suggesting here (or anywhere else) that the result of `-retainCount` itself be used. (I guess the parallel retain count I've demonstrated here can be seen as making that suggestion.) – jscs Mar 16 '12 at 06:06