How to find the cause of a malloc “double free” error?

后端 未结 13 786
情深已故
情深已故 2020-11-27 09:51

I\'m programming an application in Objective-C and I\'m getting this error:

MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double

相关标签:
13条回答
  • 2020-11-27 10:35

    This is what the malloc_error_break breakpoint looks like in the Breakpoints window in Xcode. Need to check the boxes to make it work.

    alt text http://www.martijnthe.nl/wp-content/uploads/2009/08/Afbeelding-1.png

    0 讨论(0)
  • 2020-11-27 10:37

    Check your classes and look under the dealloc method. Make sure you care calling [super dealloc].

    I had this exact same problem and found out I was calling [self dealloc] instead. Just not paying attention.

    0 讨论(0)
  • 2020-11-27 10:38

    For me the issue was solved by

    (gdb) call (void)_CFAutoreleasePoolPrintPools()
    

    right after the crash. The address at the top of the stack was the address of the culprit. Threw in a retain and voila.

    The address given in the log message did not get me anywhere. It never showed up in any of the various Instrumets. Apparently a pointer to some internal data which had already been freed.

    0 讨论(0)
  • 2020-11-27 10:39

    When an object is "double-freed", the most common cause is that you're (unnecessarily) releasing an autoreleased object, and it is later autoreleased when the containing autorelease pool is emptied.

    I've found that the best way to track down the extra release is to use the NSZombieEnabled environment variable for the affected executable in Xcode. For a quick rundown of how to use it, check out this CocoaDev wiki page. (In addition to this page, Apple has documented some incredibly obscure yet useful tips for debugging code in Xcode, some of which have saved my bacon more than a few times. I suggest checking out this Technical Note on developer.apple.com — link jumps to the section on Cocoa's Foundation framework).

    Edit: You can often track the offending object down within the Xcode debugger, but it's often much easier if you use Instruments to assist you. From Xcode, choose Run → Start With Performance Tool → Object Allocations and you should be able to trace the offending object back to where it was created. (This will work best if you're enabled zombies as discussed above.) Note: Snow Leopard adds a Zombies tool to Instruments, accessible from the Run menu as well. Might be worth the $29 alone! ;-)

    There is also a related SO question here.

    0 讨论(0)
  • 2020-11-27 10:39

    This is usually caused by some inspector, such as safari or safari preview. Refer to post or post and question.

    Remove the select of AutoMatically Show Web ...., will remove this issue.

    Note, just close safari or safari preview will not remove this issue. And you have to deselect both of safari and safari preview.

    If this will not do, refer to this answer or post to debug it.

    0 讨论(0)
  • 2020-11-27 10:42

    If malloc_error_break is not helping...

    The best way to solve this error is to run instruments with the NSZombies turned on. Instruments will flag you when the Zombie is messaged and you can trace directly back to the line of code.

    Snow Leopard required, what a lifesaver though!

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