Malloc: Heap corruption detected

I’m trying to migrate a working game from 2.2.6 to 3.17

It crashes with:

malloc: Heap corruption detected, free list is damaged at …

When the game runs in Xcode on the simulator of iPhone XR.

It happens each time in a different code segment after some extensive small memory allocations.

Any ideas?
Any procedures to detect the problem?

It’s always a tricky one to track down, but there is a lot you can do to find it.

Do you have a dSym file being generated by Xcode? If not, generate one, and read up on how to utilize it.

Turn off any compilation optimizations as well to make it easier to analyse the issue.

If the game actually runs, then you should be able to put breakpoints in locations just before you know the issue will occur, and go from there.

If it crashes on start-up, then that’s fine as well, because you know for a fact that it should be perfectly fine up to the point that your code starts. For example, put a breakpoint in AppDelegate before you set up the first scene, and follow it from there.

If you have a Windows installation, then try to compile it with Visual Studio as a win32 build, and see if you can reproduce it on there too.

In Xcode, run your application with Address Sanitizer.

If you are not sure how to do this, do a quick Google search. Address Sanitizer will let you find memory problems relatively easily.

Basically Edit the Scheme, enable Address Sanitizer, recompile, and run application in debug mode.

2 Likes

Excellent !!!
Address Sanitizer did the job.
It found an autorelease object that was released manually,
i.e. it was released twice.
Thanks a lot.

1 Like