views:

5787

answers:

4

I appear to have some overzealous releasing going on in my obj-C app - getting error message "-[myobj release]: message sent to deallocated instance 0x5633b0". I know the class of the object instance causing the problem, but this class is used all over to create many instances.

My thought is I could put some logging in the init method of the class to log whatever "0x5633b0" corresponds to which should help me track down where the instance is being created.

What exactly is the "0x5633b0" and is there any way I can get access to that value in the code to log it?

Thanks.

+6  A: 

0x5633b0 is likely the address of object in question (the value of self). You can use NSLog or printf with %p to print it.

Logan Capaldo
That was it - I just needed a way to get the address of the object somehow. I added:NSLog(@"INIT %p", self);to my init method and was able to tell which instance was causing the problem.Thanks.
JBScroggins
this is useful thanks.
kukudas
+1  A: 

You're not managing your memory properly -- you're calling release/autorelease on some object more times than you're calling retain. Make sure you're following all of the rules laid out in the Memory Management Programming Guide for Cocoa.

0x5633b0 is just the address of the memory location at which the object is stored. One thing you can try to do is to add some code to the init method:

- (void) init
{
    if(self == (MyClass*)0x5633b0)
        NSLog(@"Allocated object at address 0x5633b0");  // put a breakpoint on this line
    // do rest of init...
}

If you have any other init methods (e.g. initWithCoder:, which is called for objects instantiated from a XIB), make sure to put this snippet in those methods as well. Put a breakpoint on the NSLog line, and then see when it gets hit. Note that it may get hit several times, if an object is allocated at that address, deallocated, and then another object happens to be reallocated at the same address. The last hit before the crash is the one you want.

Adam Rosenfield
+3  A: 

In the debugger, type info symbol 0x5633b0 and you'll get some indication as to what object it is. One other thing that might be helpful is backtrace which will give you a stack trace. All in all, this blog entry has some great tips.

bbrown
+5  A: 

What worked best for me when I ran into similar problems recently was the following:

  1. Under under Project->Edit Active Executable -> Arguments tab -> Environment variables section I added and set to YES the following variables: NSAutoreleaseFreedObjectCheckEnabled, NSZombieEnabled NSDebugEnabled.

  2. Under the Run menu, I selected "Enable Guard Malloc"

With these settings the debugger provided more hints on what's wrong with my code.

(I found these tips here)

Good luck, Ori

Ori