Objective-C “message sent to deallocated instance 0x5633b0”

后端 未结 7 1901
我寻月下人不归
我寻月下人不归 2020-12-12 14:07

I appear to have some overzealous releasing going on in my obj-C app - getting error message

\"-[myobj release]: message sent to deallocated instance

相关标签:
7条回答
  • 2020-12-12 14:28

    Consider using the NSZombieEnabled flag.

    You will then know what is this deallocated object you're sending a message.

    0 讨论(0)
  • 2020-12-12 14:32

    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 and 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

    0 讨论(0)
  • 2020-12-12 14:36

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

    You can also use the instruments profiler to find the deallocated object.

    1. Start the profiler:

    enter image description here

    2. Select the "Zombies" and start the profiler.

    enter image description here

    3. Click through the simulator until you hit your "deallocated error case"

    enter image description here

    0 讨论(0)
  • 2020-12-12 14:39

    you can also add these to environment variables:
    MallocStackLoggingNoCompact 1

    and write in the gdb console:
    info malloc-history <paste-address-here>

    Reference: here

    0 讨论(0)
  • 2020-12-12 14:39

    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.

    0 讨论(0)
  • 2020-12-12 14:44

    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.

    0 讨论(0)
提交回复
热议问题