Alex Rivera | Logout

Crash when running application due to existence of unexecuted code in source file - c++

Asked 2011-03-03T00:45:12.043
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

Edit
Report

2 Answers

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.

answered 2011-03-03T00:58:19.540
0

could you try:

void Foo()  
{  
    std::vector<int> blah;
    blah.reserve(5);
    blah.begin();  
} 
answered 2011-03-03T01:17:00.523

Your Answer