Question

I'm facing 'ABORTING: HEAP MEMORY CORRUPTION' problem on Android NDK environment.

If I backtrace with ndk-gdb, it is mainly occurring on malloc/dlfree functions in libc.so and after long hours of tracing the problem, it mostly happens inside sqlite3_xxx function calls, which absolutely working fine on iOS env.

I just can't find where I have to go deep.

Have anyone encountered similar problem and fixed?

Was it helpful?

Solution

  1. I have seen memory problems, but not 'ABORTING: HEAP MEMORY CORRUPTION' that you report.

  2. You have to find out which heap is corrupt: the Java one or the C/C++ one. Or it maybe your sql. If the log is not informative, you may try to locate the error message in the binaries.

  3. If it is the C/C++ heap, what worked for me was replacing the standard malloc/calloc/free with my own versions.

    #define malloc(x) myMalloc(x, __FILE__,__LINE__,__func__)
    

    and so on; myMalloc() and friends print debug information so that you can find out where memory was allocated and freed. I had the source of the library and could compile it. Then logging, logging, logging...

    #include <android/log.h>
    #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "~~~~~~", __VA_ARGS__)
    #define DLOG(...) __android_log_print(ANDROID_LOG_DEBUG  , "~~~~~~", __VA_ARGS__)
    

    I also made myMalloc() zero the allocated memory -- just in case. One more trick is to allocate a larger chuck and put a guard value in the end of it. If that value gets corrupt -- you see.

  4. If it is the Java heap, you will have to log your native function calls (I myself have never seen problems in the Java heap, usually Java complains about its JNI-specific stuff).

OTHER TIPS

For my program, 'ABORTING: HEAP MEMORY CORRUPTION' shows when there are thread safety issues. Specifically with Cocos2d-x framework, its getFileData() function of ZipUtils may crash when loading .plist atlas and addImageAsync() at the same time on Android. Though the codes works fine on iOS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top