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

后端 未结 13 789
情深已故
情深已故 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:43

    I just want to add my experience in addition to the answer of Quinn Taylor.

    In one of my apps, I have to parse and save data into core data objects and later on get these objects to display on the views. In fact, the app works just fine and does not crash at all, until I tried to do a stress test of navigating back and forth multiple times, tried to open multiple views as fast as possible. The app crashes with the above message.

    I have tried all the methods that Quinn suggested in his answer and still failed to find out where was the exact cause.

    I set NSZombieEnabled=YES, and NSStackLogging=YES, ran the command shell malloc_history to find out why, but still no luck. It always points out to where I save the data into core data objects, in fact, I have checked thousand times the over released objects there, nothing odd.

    Running in Instruments with various tools(Allocations, Leaks, etc...) still did not help. Enable the Guard Malloc still got nothing.

    Final rescue: I tried to come back to the views where the objects were taken from Core Data and sent a retain message to all of these objects, and took note to these changes. It solved the issue!!!

    So, I found out that I failed to retain one, that's exactly the cause. Just want to share my experience so you have another rescue for your app.

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

    You'll find out what the object is when you break in the debugger. Just look up the call stack and you will find where you free it. That will tell you which object it is.

    The easiest way to set the breakpoint is to:

    1. Go to Run -> Show -> Breakpoints (ALT-Command-B)
    2. Scroll to the bottom of the list and add the symbol malloc_error_break
    0 讨论(0)
  • 2020-11-27 10:47

    In Xcode, click left of the line number to set a breakpoint. Then you can launch it by doing a "Build and Debug".

    It is recommended to not have object that you create be autorelease since memory is a commodity on the iPhone. Apple recommends explicitly calling release.

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

    Open up the debugger console by pressing Cmd+Shift+R. There, type

    break malloc_error_break
    

    to set a breakpoint at the beginning of the malloc_error_break function.

    If you want to find out what object is located at address 0x1068310, you can type the following into the debugger console:

    print-object 0x1068310
    

    Of course, you have to do this while the object is still alive -- if the object has already been freed by the time you do this, then this will not work.

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

    Please find the below steps for how to find the object which is free and crash the application.

    1) Click on the "Breakpoint navigator".
    2) Then click on the "+" button which is below.
    3) Add the "Symbolic Breakpoint..." from the list.
    4) Add the "malloc_error_break" keyword on the "Symbol" option.

    Or you can also refer the below GIF presentation.

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

    Adding a symbolic breakpoint in Xcode 4

    Just an update to make this relevant to Xcode 4...

    From the Xcode 4 User Guide:

    To add a symbolic breakpoint . . .

    1. In the bottom-left corner of the breakpoint navigator, click the Add button.
    2. Choose Add Symbolic Breakpoint.
    3. Enter the symbol name in the Symbol field.
    4. Click Done.
    0 讨论(0)
提交回复
热议问题