This Memory Management Tip Could Save You Hours of Frustration!

Matthew Campbell, April 30th

Dealing with memory on the iPhone is one of the most difficult
challenges that you will face as a new iPhone developer. Today,
I am going to show you a very simple tip that will help you
debug the memory problems that you are likely to face.

One thing that you have to worry about with memory management is
the “retain count” of an object. The retain count is how the
system keeps track of the memory used by an object. If an
object’s retain count is zero and your attempt to access it
your app will crash; if you do not make sure the retain
count of your object is 0 when the object goes out of focus you
will have a memory leak.

Clearly, it is important to be able to find out what the current
retain count of an object is. Here is how you can do this:

This writes out the current retain count to the log. The
important function is [someObject retainCount].

That is it – know how to find out the retain count of a particular object will give you some help in the fight against randomly crashing (or leaky) apps.

[...] of Frustration! April 30, 2009, 6:09 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/04/this-memory-management-tip-could-save-your-hours-of-frustrati...) Dealing with memory on the iPhone is one of the most difficult challenges that you will face as a [...]

This Memory Management Tip Could Save You Hours of Frustration!…

You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

I define the following macro in .gdbinit:

define rc
call (int)[$arg0 retainCount]
end

When I’m stopped at a breakpoint, I can simply type rc and the name of the object. It can also be set as a breakpoint action.

This is what Apple say about RetainCount:

“Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.”

Personally I’ve never had much success with it for the reasons Apple describes but in some circumstances it can be helpful.

Michael.

This is MM “tip” is *really* incorrect. Look at cocoa-dev lists and you can find any number of responses from Bill Bumgarner emphatically stating do *not* rely on retainCount to give you any kind of accurate idea as to what your objects’ MM status is.
Use manual tracking per Apple’s MM rules, backed up by Instruments and clang.

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.