开发者

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

开发者 https://www.devze.com 2023-01-01 19:02 出处:网络
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开发者_Python百科 the current refcount of an object derived from NSObject.How

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开发者_Python百科 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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号