2

When i run this code in debug mode no exceptions appear but in release mode i get this exception :

Unhandled exception at 0x768b4b32 in RealTimeSLT.exe: Microsoft C++ exception: cv::Exception at memory location 0x003de734..

why this problem appear only when release ?and how can i fix it ??

FileStorage fs2(fileName, FileStorage::READ);
fs2.open(fileName, FileStorage::READ);
fs2["Mat"] >> Mat;  
fs2["dMat"]>> dMat; 
fs2.release();
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
RBM
  • 73
  • 3
  • 7
  • The OpenCV cv::FileStorage class throws an exception when it cannot open the file. You'd be wise to catch that exception and tell the user to pick another file. Or use the proper path name of the file in your code. – Hans Passant May 20 '13 at 12:22
  • You don't need to call `open()` as the line before already called it with the specified parameters. Basically, you're opening, closing and re-opening the file right now. – Niko May 20 '13 at 12:35
  • If the path isn't explicit but relative to the working directory, it won't find it when you run the executable is a different directory. Sometimes Debug and Release use different working directories. Check the project configuration. – Peter Wood May 20 '13 at 12:53
  • In VS debug builds, uninitialized bare pointers are usually initialized to null on your behalf. If you have any bare pointers in your code which you forgot to initialize to null, they will point to garbage locations in release builds, and dereferencing one would be undefined behaviour. – Oktalist May 20 '13 at 13:11

4 Answers4

2

Assuming you're running in VS, choose Debug->Exceptions and click on 'Break When Exception is Thrown'

Run the app under the debugger and see why that exception is thrown and what its complaining - it's most likely something different in the environment the app is running under.

If you need to examine variables then release build makes this hard as the optimiser is likely confusing the debugger. You can probably solve this by turning off optimizations in your release build. However, if its a timing issue this may hide the problem.

Another option is catching the exception being thrown and then logging its internal message - this is normally a function called 'what()' or similar. This very likely will point you to the actual problem. Its likely that you'll want to catch this exception anyway.

If its undefined behavior causing the difference between Release and Debug then its likely the above wont be as much use.

Mike Vine
  • 9,468
  • 25
  • 44
0

The code looks odd.

Without knowing any more about your FileStorage object, I can see that you've created one on the stack on the first line.

Assuming release() is some kind of reference counting method, fs2.release() will attempt to delete it since the reference count will be zero.

Not good to delete objects that have been allocated on the stack. You'll get a crash.

Either (1), try this instead (i.e. allocate on the heap)

FileStorage fs2 = new FileStorage(fileName, FileStorage::READ);

(Assuming the object has reference counting semantics built in; check the docs).

Or 2: remove the last line as fs2 will go out of scope as the stack unwinds.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • While this may be good advice in general cases, you're wrong in this specific one: OpenCV's `FileStorage::release()` does not delete the object but closes the file and deallocates the internal buffer. It's perfectly fine to call it after having finished reading from a file, see http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html – Niko May 20 '13 at 12:33
  • Odd that the debug and release behaviour differs so much though. That's why I barked up the wrong tree thinking about debug and release heaps. – Bathsheba May 20 '13 at 12:36
0

U need to add "*.lib" files to vs project's linker again when U r in release mode. I think this is a bug for opencv after 2.4.1.

0

In my case, it was because the OpenCV libraries were built with VS2010, and I was using VS2015.

To resolve issue, I changed my Project Properties > General > Platform Toolset to match the toolset used to build the opencv libraries I was linking with.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Mark Klink
  • 21
  • 3