简体   繁体   中英

Inspecting the refcount of an NSObject in the Xcode debugger on iPhone?

This should be so simple, and yet, it is evading me: When stopped at a breakpoint in the Xcode/gdb debugger, I wish to find the current refcount of an object derived from NSObject. How can I do this? I can't seem to find a way, and it is driving me mad.

Even better: go to the console (command+shift+r) and type:

p (int)[objectName retainCount]

And press enter. You can execute any statement you want on that console (it's GDB, at least for now), making whatever code calls you want, and it'll still try — and usually succeed — to come up with a result. 'p' is to print the result as a C primitive, 'po' would print the result as an Objective-C class (ie, by calling 'description' just as if you'd NSLog'd the instance). The 'int' is there because GDB can't always determine the return types at runtime, in which case you need to tell it.

It's as simple as

NSLog(@"retainCount:%d", [objectName retainCount]);

where objectName is whatever you named the object in question.

Please note that this does not work on NSStrings since they are managed differently.

For more info: http://www.karlkraft.com/index.php/2009/04/22/dont-use-non-mutable-objects-to-understand-leak-detection/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM