54

I have a big problem with my iOS App: it crashes sometimes without detailed debug error. The stack trace is empty. These are the only two lines in the stack trace:

  1. crash start in UIApplicationMain at "symbol stub for: -[_UIHostedTextServiceSession dismissTextServiceAnimated:]".
  2. and report "libsystem_c.dylib`malloc_error_break".

in com.apple.main-thread.

The error on Xcode debugger (with connected device):

malloc: *** error for object 0x208a7614: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

I have set a breakpoint in malloc_error_break with libsystem_c.dylib without any feedback from debugger. I have no idea to solve this issue.

Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
  • 1
    This is hard. You basically have to inspect the code to find the problem. If you can figure out what kind of object is involved, that helps a lot. (BTW, ARC or manual reference counting?) – Hot Licks Nov 07 '13 at 18:41
  • I'm pretty sure the only utility of setting a breakpoint in `malloc_error_break` is that it'll give you a chance to look at the corrupted freed object, and the content of the memory may help you trace the point where you're overwriting it. – This isn't my real name Nov 07 '13 at 19:45
  • It's a C++ library an the project it's MRC. –  Nov 08 '13 at 07:48
  • My friend compiled my C code on a Mac, and it gave this error on runtime. However, it compiled and ran just fine on Linux. It seems to be a Mac-specific issue. – Geremia Feb 02 '16 at 21:05
  • Try just clean the project CMD+SHIFT+K. it helped :) – korgx9 Apr 26 '18 at 06:56

5 Answers5

52

To find the source of the problem, in Xcode go to Product > Scheme > Edit Scheme, and under Diagnostics tab enable all the Malloc settings and Guard Malloc.

With that, run your application again, and Xcode will stop at the line causing the problem.

Scheme definition

idmean
  • 14,540
  • 9
  • 54
  • 83
Pedro Soares
  • 1,117
  • 12
  • 14
  • 20
    It's worth pointing out that Guard Malloc is only available in the simulator and not on a real device. See: https://developer.apple.com/library/ios/documentation/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html – MBulli Jan 20 '15 at 23:19
  • 2
    dyld: could not load inserted library '/usr/lib/libgmalloc.dylib' because image not found – onmyway133 May 03 '16 at 14:02
  • 1
    Enable Guard Malloc worked for me although it's worth noting that it appeared to slow down the simulator significantly, thus I would only turn it on when debugging an actual issue. – arcady bob Jul 04 '16 at 22:32
  • I'm using macOS Big Sur 11.1, Xcode 12.3, when I tried Xcode, Product > Scheme > Edit Scheme, nothing happens, I've tried several times, even rebooted then tried again, same again, nothing happens. – SPlatten Dec 30 '20 at 15:49
30

Since you're in the debugger, you should look at the memory location 0x208a7614 and see what's there. The data in memory may be helpful in figuring out what's going wrong.

What's happening is one of the following:

  1. you are freeing an object twice,

  2. you are freeing a pointer that was never allocated

  3. you are writing through an invalid pointer which previously pointed to an object which was already freed

Since the stack trace is coming up empty, it might be useful to add some debugging log statements to your code at various places to see if you can narrow down where in the code the problem lies. Using the memory tools in Instruments might also help. You could try turning on NSZombies, but this looks like a C allocation problem and not an Objective-C one.

Also, is anything else written to the console before the crash? If so, it may point you to where the problem is coming from.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • 1
    There's a third possibility suggested by the error message: OP is writing through an invalid pointer which previously pointed to an object which was already freed. – R.. GitHub STOP HELPING ICE Nov 07 '13 at 16:32
  • Ah, good point. I was thinking the error was displaying on a call to `free()`, but it's on a call to `malloc()`. Good catch. I'll update the text to reflect that. – user1118321 Nov 07 '13 at 18:09
  • 12
    Fourth possible cause: Writing past the end of an allocated block into another, currently-unallocated block. – This isn't my real name Nov 07 '13 at 19:43
  • 1
    If Zombies don't catch anything then Guard Malloc may be able to help. – Greg Parker Nov 07 '13 at 20:52
  • This answer is what saved me in my C program. I had accidentally allocated too small of an array to hold a sprintf result somewhere else in my code. Checking the memory address told me exactly which was the offending string because I was able to recognize its content. – sudo Apr 26 '14 at 21:29
  • The 3rd scenario is what got me in my C program. I called free() on a pointer used by getline(), but used it again afterwards. (by getline()) – Jerfov2 Nov 04 '15 at 00:04
  • another possibility: like ```malloc(-2013003776)```, should ```malloc(ntohl(-2013003776))``` – bianbian Jun 03 '18 at 13:14
2

hi guys i have found this solution if you are using nib or xib interface and you facing this problem when you want to push a viewcontroller object then some time this error will occur and your app will be crash (specially error in iPad) Here is the solution:

// Format like this

UINavigationController *nav=[[UINavigationController      alloc]initWithRootViewController:yourViewControllerObj];

[self.navigationController  presentViewController:nav animated:true completion:nil];

Don't try to push in this condition.

shashi Gupta
  • 141
  • 1
  • 11
Ravi Raja Jangid
  • 795
  • 9
  • 16
0

If you have this problem. go to: product->scheme->diagnosis-> then enable mollic gaurd edge and zombie object then close then go product->stop then again product-build and run. best ofluck

0

I' am having the same issue.

I have a macOS 2015, big sur 11.7.

It all started when I updated my xcode. It seems that the last xcode version available for big sur has some bugs.

The fortran code that calls c++ and python plots was not running

I downgrade the xcode to 12.5, and the comand line tool for the this xcode version, but still have the same problem.

And it is intermitent. Some times code generate the error and sometimes not...