sorin
sorin

Reputation: 170420

How to detect the object being double released with Xcode - message sent to deallocated instance?

I am relatively new to Xcode4 and I would like to know how can I identify a double release with it.

In the debugger I see a line like *** -[NSConcreteMutableData release]: message sent to deallocated instance 0x60b63fe0.

The problem is that knowing the address doesn't help a lot identifying the object and also the object type doesn't help too much identifying it.

I read http://www.friday.com/bbum/2010/01/10/using-malloc-to-debug-memory-misuse-in-cocoa/ but I did not found this to be too successful.

Upvotes: 0

Views: 2367

Answers (5)

Conor
Conor

Reputation: 1777

Use the profiler for Zombies to track down the actual object. It will automatically enable NSZombies and more importantly keep a history of all release and autorelease messages.

  1. Use Profile menu command under Product.
  2. In the profiling template selection dialog that appears, select Zombies.
  3. Click the Record button in the toolbar (Command-R) to begin recording. Use your app normally. If a call is made to a deallocated object, a flag is inserted in the timeline pane and a Zombie Messaged dialog appears.
  4. Click the focus arrow next to the zombie’s memory address to display the memory history of the zombie object in the detail pane, along with corresponding reference counts and method calls.

Here is the apple documentation with pictures:

https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/EradicatingZombies.html

Upvotes: 0

Nathan Day
Nathan Day

Reputation: 6037

You can replace the release method using categories for testing purposes, this is not designed to work like that as part of the language but in the past I have found success it trying to do some testing, usually all you find out is that an autorelease pool is releasing you object.

Upvotes: 1

m.voong
m.voong

Reputation: 61

You can enable the NSZombieEnabled environment variable - see How do I set up NSZombieEnabled in Xcode 4? for instructions on how to do this on Xcode 4.

What this means is that released objects are kept around in memory, so the debugger can still find out the type of objects. When the crash occurs, you are told of the object in question.

The "Zombies" Instruments tool is great for detecting bugs of this type - it actually enables NZZombieEnabled and you can use it to find out exactly which line of code the crash occurs.

Upvotes: 6

Peter Osburg
Peter Osburg

Reputation: 276

Try using the property retainCount. If an object has a retainCount == 0 then it will be freed. Ultimately you will not be able to send it a release message.

Upvotes: -1

mayuur
mayuur

Reputation: 4746

You are sending a release message to something which already is released or having retainCount 0. Hence, its giving such an error message. Actually, my answer over here might help.

Upvotes: 0

Related Questions