views:

41

answers:

3

I think i have an UIImage that has a higher retain count than it should have and i am probably leaking memory. I use this image as a thumbnail, to set a custom background to a uibutton. So the uibutton is holding a reference to it and so do i. But instead of 2, the retainCount is 3. Do i have to create a custom UIImage derived class and override dealloc if I want to place a log message there and then change the class used from UIImage to my class, or is there an easier way. Thanks in advance.

+1  A: 

I would suggest you use the "Leaks" tool in Instruments. It will tell you if you have a leak or not and give you all the information you need.

Update:

I've just been watching a WWDC 2010 video "Future Proofing your Application" where the Apple engineer states that on OS 2.x [UIImage imageNamed:] actually leaks with a retain count 1 more than it should be. So if your device is running iPhone OS 2.x then that would be why!

Mike Weller
@Mike is right, plus you shouldn't ever look at the retain count of objects. There's a lot of valid reasons why that UIImage has a higher retain count than you expected. If the Leaks tool isn't showing you a leak, don't worry about it.
kubi
I tried using the leaks tool, it made me even more confused. I'm in the process of understanding the leaks from instruments. The "leaks" it shows me are very unlikely to be leaks. Well, even an empty app with a few textfields and switches is shown as leaking. No code added.
kudor gyozo
+1  A: 

Do you know the object is leaking? It's pretty much always a bad idea to leap to conclusions based on retain counts. Use Build & Analyze, Leaks and so on to determine if you've a problem.

If you really want to subclass and log dealloc, you can, but what is it actually going to tell you?

walkytalky
+2  A: 

Use a category on the targeted class to override dealloc and set a breakpoint on it.

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html

fraca7
Note that this is extremely dangerous and highly discouraged. From that same document: "Although the language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from using this functionality.".
Brad Larson
In production code, certainly. To debug, I'll use everything that makes my life easier :)
fraca7