Tuesday, February 8, 2011

The case deserves your attention: incorrect checksum for freed object

When I port my free screen capture tool DuckCapure to Mac OS. I got an exception every time when I start to capture the screen. The error message is:
DuckCapture(10059,0x7fff7006aca0) malloc: *** error for object 0x103468168: incorrect checksum for freed object - object was probably modified after being freed.

*** set a breakpoint in malloc_error_break to debug

The program has unexpectedly finished.

I'm very confused with this assertion, the error happens when I use the data member from an internal class called DWidgetData, the constructor is as follows:
        : m_pStyledContainer( NULL )
        , m_pContainerContainer( NULL )
        , m_pLayout( NULL )
        , m_pAnimationFading( NULL )
        , m_pEffectOpacity( NULL )
        , m_pEffectDropShadow( NULL )

The debugger shows a very strange content for a just initialized DWidgetData object:
Looks like the data corruption, or the constructor is never called. I have another test application which used the same component and passed the test, but DuckCapture failed. I spent hours on this issue, and finally resolved it:

Reason: In dynamic linked library DuckCore, I used forward declaration for DWidgetData in header file, and defined the class in source file. Looks reasonable, huh? However, I happened to define another DWidgetData in DuckCapture also. That's the nightmare. The code in DuckCore used the DWidgetData in DuckCapture instead of the one defined in the source file I used it.

This is another dangerous case on compiler compatibility: Visual Studio 2008/2010 on Windows works, but gcc 4.2 on Mac OS does not.

No comments:

Post a Comment