10

I'm working on a pretty tricky problem that I've been on for literally a week now. I've hit a very hard wall and my forehead hurts from banging it so I'm hoping someone can help me out.

I am using Visual Studio 2005 for this project - I have 2008 installed but was running into similar issues when I tried it.

We have an application currently working compiled against OpenCv1.1 and I'm trying to update it to 2.2. When we switch over statically link to the new libs, the application crashes - but only in release mode. So dynamic linking and debug both work fine.

The crash is in std::vector when calling push_back.

I then came up with a sample test application which runs some basic code in opencv which works fine and then took that exact same code and added it to our application. That code fails.

I then gutted the application so it didn't instantiate any code objects except the main gui and 1 class which called that code and it still crashed. However, if I ran that code directly in the main gui, it worked fine.

I then started commenting out huge amounts of the application (in components that should never be instantiated) and eventually I worked my way down down down until...

I have a class that has a method

void Foo()  
{  
    std::vector<int> blah;  
    blah.begin();  
}  

If this method is defined in the header, the test code works, but if this code is defined in the cpp file, it crashes. Also, if I use std::vector<double> instead of int, it also works.

I then tried to play with the compiler options and if I have optimizations turned off (/Od) and Inline Function Expansion set to Only __inline (/Ob1) it works even with the code being in the cpp file.

Of course, if we go back to the ungutted application and change those compiler options by themselves, it crashes.

If anyone has any insights on this, please let me know.

Thanks, Liron

6
  • When you step through it with the debugger is the program able to load completely or does it crash before it even gets to main(or WinMain)? Your problem's interesting because usually I find dynamic linking to be more problematic than static linking to the runtimes
    – greatwolf
    Mar 3, 2011 at 0:53
  • The program totally loads fine. Then I press a button on the (Qt) gui which runs the code calling into opencv. If the code is right there in the button press it works, but if the code is called from another class, it crashes.
    – Liron
    Mar 3, 2011 at 1:17
  • I updated the project to not create a gui at all and it still crashes once I call the opencv code.
    – Liron
    Mar 3, 2011 at 2:08
  • is there any difference if you compile it with/without optimizing (nodebug)?
    – fazo
    Mar 3, 2011 at 4:08
  • @fazo If I compile without optimizing AND with Inline Function Expansion set to Only _Inline then it doesn't crash. If either of those conditions are not true it crashes.
    – Liron
    Mar 3, 2011 at 6:27

3 Answers 3

8

ARGH! Solution figured out.

In our solution we had defined _SECURE_SCL = 0, but in the 3rd party libs we had build, that was undefined (which means = 1). Setting _SECURE_SCL to 0 supposedly reduces runtimes drastically, but it has to be done the same across all included libs otherwise they will treat array sizes differently.

http://msdn.microsoft.com/en-us/library/aa985896%28v=vs.80%29.aspx

That was a fun week.

6

The STL classes, like vector<>, have a layout mismatch between the release and the debug builds, caused by iterator debugging support. Your problem behaves exactly like the kind of trouble you get into when you link a debug build of a .lib or DLL in the release build of your application and exchange an STL object between them. Heap corruption and access violation exceptions are the result.

Triple check your build settings and ensure that you only ever link the release build of the .libs in your Release build and the debug build of the .libs in your Debug build.

6
  • +1. Just to add one possibility: mixing statically linked DLLs/EXEs with dynamically linked ones and passing around pointers without them carrying the information how they can be destroyed properly. Mar 3, 2011 at 1:23
  • We should be doing everything dynamically.
    – Liron
    Mar 3, 2011 at 2:04
  • The libs we're including are: Imm32.lib QtCore.lib QtGui.lib QtMain.lib QtNetwork.lib QtOpenGL.lib QtXml.lib Vfw32.lib WinMM.lib comctl32.lib glu32.lib libjasper.lib libjpeg.lib libpng.lib libtiff.lib opencv_calib3d220.lib opencv_contrib220.lib opencv_core220.lib opencv_features2d220.lib opencv_ffmpeg220.lib opencv_flann220.lib opencv_gpu220.lib opencv_haartraining_engine.lib opencv_highgui220.lib opencv_imgproc220.lib opencv_lapack.lib opencv_legacy220.lib opencv_ml220.lib opencv_objdetect220.lib opencv_video220.lib ws2_32.lib qjpeg.lib Also, we are linking boost, which should handle this
    – Liron
    Mar 3, 2011 at 2:04
  • hmm .. STL are pure TEMPLATE with no runtime libraries .... only the stlport does some io stuff in dlls ...
    – vrdhn
    Mar 3, 2011 at 5:50
  • @Vardhan: yes, but when you pass a vector to a method of another library, it presumes a layout. That is, using the vector per se is good, but passing it around DLL boundaries, in case of mismatch of layouts, causes havoc. Mar 3, 2011 at 7:41
0

could you try:

void Foo()  
{  
    std::vector<int> blah;
    blah.reserve(5);
    blah.begin();  
} 
3
  • Adding reserve(5) makes no difference. Still crashes. The code in that function is never executed anyway (at least the debugger never hits it).
    – Liron
    Mar 3, 2011 at 1:22
  • @LKIM then try std::vector<int> blah(5);
    – fazo
    Mar 3, 2011 at 1:37
  • We used to have that - still caused a crash :-\. The code above was just me whittling the crash down to the most minimal that I could, but that used to be part of a function that did a whole lot more.
    – Liron
    Mar 3, 2011 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.