KexAri KexAri - 10 months ago 93
iOS Question

Malloc errors in Swift

I'm suddenly getting weird Malloc errors in my code such as:

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

It seems that they are random and just appear in different places when I set break points. I'm on XCode 8.1. Swift 3.0. Has anyone come across this?

I set the breakpoint and I get this:

-> 0x112fa4760 <+0>: pushq %rbp
0x112fa4761 <+1>: movq %rsp, %rbp
0x112fa4764 <+4>: nop
0x112fa4765 <+5>: nopl (%rax)
0x112fa4769 <+9>: popq %rbp
0x112fa476a <+10>: retq

Answer Source

The most common cause of this error is threading violations. For example, if one thread performs the final release on an object and begins to deallocate it, and then another thread which has an unowned (Swift) or assign (ObjC) reference to the object modifies it, that will trigger this error. (It should not be possible to get this error if all references are strong or weak, since it shouldn't be possible for the object to be deallocated in the former case, and weak references are thread-safe in the latter case.)

It's of course possible that this is exposing a bug in the latest SDK (and so you should open a radar), but it is more typical that it's a bug in your code that changes in timing have caused to manifest. As noted, you should set a symbolic breakpoint on malloc_error_break to find out what object is in question.