Cocoa debugging tip

When you’re debugging a memory allocation issue, it’s very useful to be able to see the retain count of an object. One way to do it is by calling the object’s retainCount method, which you can do in the debugger console by entering something like call (int)[self retainCount].

That’s a lot of typing if you do it very often. Fortunately GDB lets you create macros that can make it a lot easier. To define a macro called rc, which will print the retain count, you can define a macro as follows:

define rc
call (int)[$arg0 retainCount]

You can now type rc self to see the current object’s retain count. Unfortunately you’ll have to enter that definition every time you start the debugger.

However, GDB lets you create a startup file with commands that will be executed every time it starts up. Simply create a file called .gdbinit in your home directory and enter that definition in it. You will now have the command rc available every time you use the debugger.

1 thought on “Cocoa debugging tip”

Leave a Comment