Page 1 of 1

Heap corruption problem

Posted: Tue Mar 10, 2009 12:21 pm
by Veehmot
If I comment out the line that is calling cvConvertImage(), it works well. Otherwise, I get this error:
"Windows has triggered a breakpoint in cvsample.exe.
This may be due to a corruption of the heap, which indicates a bug in cvsample.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while cvsample.exe has focus.
The output window may have more diagnostic information."

Through a web search I realized that this can be caused by a dll incompatibility.

Please note that:
A. The error is triggered in the function GetFrameImage() from Optitrack SDK.
B. The function is NEVER called, but it throws me that error anyway
C. I'm using Visual Studio Team System 2008.

Build log:

Debug log:


Re: Heap corruption problem

Posted: Tue Mar 10, 2009 1:36 pm
by beckdo
GetFrameImage() needs to be called very carefully because you're providing it with a buffer that the OptiTrack SDK will fill with data. Now, that being said, if the buffer you provide is not large enough you will corrupt the heap--which is happening in your case.

If tell GetFrameImage to populate your buffer at 32-bits per pixel, the buffer must be at least 640*480*4 bytes in size or you'll corrupt memory.

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 5:14 am
by Veehmot
Thanks for the reply.
That is not my case, since I'm asking for a 8-bit per pixel buffer. And the size of the buffer is right, (PixelWidth x PixelHeight x (BitsPerPixel / 8)) => (640 x 480 x (8 / 8)) = 307200. And that is the size of my buffer, 307200, so the buffer is large enough.

I realized that the problem arises only when using /DEBUG.
That being said, these are my parameters for the compiler:

Code: Select all

/O2 /Ob1 /I "../../cxcore/include" /I "../../cv/include" /I "../../otherlibs/highgui" /I "C:\Program Files\NaturalPoint\Optitrack\inc" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_VC80_UPGRADE=0x0600" /D "_MBCS" /GF /Gm /EHsc /MD /Gy /Fp".\..\..\_temp\cvsample_Rls/cvsample.pch" /Fo"C:\DOCUME~1\ELJORJ~1\LOCALS~1\Temp\\cvsample_Release.Win32\\" /Fd"C:\DOCUME~1\ELJORJ~1\LOCALS~1\Temp\\cvsample_Release.Win32\\" /W4 /nologo /c /Zi /TP /errorReport:prompt

And for the linker:

Code: Select all

/OUT:".\cvsample.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:"../../lib" /LIBPATH:"C:\Program Files\NaturalPoint\Optitrack\lib\i386" /MANIFEST /MANIFESTFILE:"C:\DOCUME~1\ELJORJ~1\LOCALS~1\Temp\\cvsample_Release.Win32\cvsample.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /NODEFAULTLIB:"libmmd.lib" /SUBSYSTEM:CONSOLE /DYNAMICBASE:NO /MACHINE:X86 /ERRORREPORT:PROMPT optitrackuuid.lib cxcore.lib cv.lib  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib "..\..\lib\ml.lib" "..\..\lib\highgui.lib"

Any suggestions? Why can't I debug an Optitrack application?

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 6:36 am
by Veehmot
Ok now without /DEBUG it crashes as well.

Here it's my code now:

That code works well, and the video buffer is displayed correctle, but when I try to use cvConvertImage() it crashes. BUT the function cvConvertImage() works if I comment out GetFrameImage().
And I tested the two functions in stand-alone projects, and they both work good. The problems arises when trying to implementate one with the other.

NOTE: It doesn't matter that I sent two identical parameters to cvConvertImage(), that's not the case, that was just for the sake of this post, because the error appears in the GetFrameImage() line.

I have to use cvConvertImage() for my project. Other functions from the OpenCV library work well, the problem it's just with cvConvertImage().

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 10:53 am
by beckdo
Can you zip up your project and send it over? I'll take a look at it.

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 11:40 am
by Veehmot ... leid=58415

There it is.. try to remove comment from line 53 in optitrack.cpp and you'll see that GetFrameImage() gets the error at runtime.

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 1:58 pm
by beckdo
Ok, I've reproduced your problem. It's not clear where the problem is just yet. I will try to track it down for you.

Re: Heap corruption problem

Posted: Wed Mar 11, 2009 4:57 pm
by beckdo
Ok, there seems to be something wrong with the way you've brought in the OpenCV library: cv.lib, cxcore.lib, highgui.lib. Something with the way these are compiled and linked in is causing problems. When stepping through your code it looks like GetFrameImage() is causing a heap corruption when the following cvConvertImage line is included, when in actuality the program is crashing before execution enters GetFrameImage().

I think we can both agree something very strange is happening, and it doesn't appear to be the code itself, so I think you should take a closer look at the way you're compiling and bringing in your external library.

Re: Heap corruption problem

Posted: Fri Mar 13, 2009 5:20 am
by Veehmot
Ok I will check them up, thanks for your time.
I will update this post for further information.

Re: Heap corruption problem

Posted: Thu Sep 17, 2009 5:55 am
by Has981
Did anybody manage to figure out whats causing this..

I've put up a post earlier about a very similar problem but couldn't find the solution.