Strange crash with exceptions - c++

When debugging a C++ program in VS 2013, I get a crash (technically a "breakpoint") in an exception handler (catch block) after an exception is thrown. The following is from the Output window after it happens:
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: _com_error at memory location 0x00C6EE24.
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: ComException at memory location 0x00C6EF04.
First-chance exception at 0x75E54878 in Program.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
HEAP[Program.exe]: HEAP: Free Heap block 00FAB5A8 modified at 00FAB5D4 after it was freed
Program.exe has triggered a breakpoint.
I tweaked the code to find the cause and got it to the point that it does something as simple as displaying a message box. Something like:
try
{
...
}
catch (const Exception&)
{
::MessageBox(NULL, L"Error", L"Error", 0); // <-- Crash happens here,
// BEFORE the message box is displayed
}
The message from the Output window suggests that I'm trying to write to freed/invalid memory, but as you can see from this code snippet, that's not the case - I'm not even using variables at the time the crash happens.
Another strange thing is that, after the crash, the Call Stack window shows frames that were already unwound due to the exception, as if the call stack got "rebuilt".
Any ideas what's wrong?

Related

SIFT detectAndCompute throws an ipp exception

In my real time image tracking solution, whenever i am calling detectAndCompute i get an exception thrown.
The exception doesnt crash the program, and the tracking still works (on my machine) but due to the fact that it is constantly throwing the exception, I am seeing some major performance setbacks. Here is the exception:
Exception thrown at 0x00007FFF0FBEA839 in OpenCV2.exe: Microsoft C++ exception: ipp::IwException at memory location 0x0000004CB72FC5C8.
I tried printing the exception to get details with a try-catch clause but it didnt give me any info. Here is the line that throws this exception:
algo->detectAndCompute(frame, mask, keypoints2, descriptors2, false);
This is harmless, see for example https://github.com/opencv/opencv/issues/9718.
If you run your code outside of the debugger you will not see the exception printout.

COleException at memory location for COleSafeArray

I'm trying to work with the following sample code that was provided to me
// select the Access levels from access level list
BSTR myBstr = SysAllocString(L"Hello World");
COleSafeArray saAccessLevels;
VARIANT vAccls;
saAccessLevels.CreateOneDim(VT_BSTR, 1);
saAccessLevels.PutElement(0, myBstr); // <-- Error after this line
vAccls = saAccessLevels.Detach();
The Visual C++ debugger breaks at the line noted in the above comment and I get the following errors:
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\olemisc.cpp(423) : AppMsg - Warning: constructing COleException, scode = E_INVALIDARG ($80070057).
First-chance exception at 0x75371D4D in MyApplication.exe: Microsoft C++ exception: COleException at memory location 0x0057FC58.
Unhandled exception at 0x75371D4D in MyApplication.exe: Microsoft C++ exception: COleException at memory location 0x0057FC58.
I am not familiar with C++ programming. What am I doing wrong?
You're using the PutElement method incorrectly. The first parameter is a pointer to an array of indices. Have a look at MSDN.

Where can I see the what() message from an unhandled std::exception in Visual Studio 2012?

Is there a way to see the explanatory string from an unhandled exception? I'm using Visual Studio 2012 Express and can't seem to find a way to see it.
When I run the following code:
#include <stdexcept>
int main(int argc, char* argv[])
{
throw std::runtime_error("warp core breach");
return 0;
}
all I get in the output window is this:
First-chance exception at 0x7652C41F in vstest.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0015F6A4.
Unhandled exception at at 0x7652C41F in vstest.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0015F6A4.
I would have expected the "warp core breach" message to be printed there. I have all options under Debugging->Output Window->General Output Settings set to On.
You'll get a window when the exception is thrown with the option to break/continue/ignore. Copy and paste the hex address this dialog reports, then click the break button. Now in a watch window, enter something like: (std::runtime_error*)(0x002cfbc8) into a cell in the first column.

how to find out what is causing "cv::Exception at memory location"?

I'm currently suffering from some strange exceptions that are most probably due to me doing something incorrectly while interacting with opencv:
First-chance exception at 0x7580b9bc in xxx.exe: Microsoft C++ exception: cv::Exception at memory location 0x00c1c624..
I've already enabled the Thrown field in the Debug -> Exceptions menu, however I really can't figure out where in my code the exception is thrown.
How can I debug this?
EDIT
the stack frame reads like this (my app won't even show up in the list!):
KernelBase.dll!7580b8bc()
[Frames below may be incorrect or missing ]
KernelBase.dll!7580b8bc()
opencv_core242d.dll!54eb60cc()
You could wrap your entire main in a try catch block which prints out the exception details. If the open CV API can throw exceptions, you will need to think about handling them anyway as part of your design:
try
{
// ... Contents of your main
}
catch ( cv::Exception & e )
{
cerr << e.msg << endl; // output exception message
}
OpenCV has this handy function called cv::setBreakOnError
If you put the following into your main before any opencv calls:
cv::setBreakOnError(true);
then your program will crash, because OpenCV will do an invalid operation (dereferencing a null pointer) just before it would throw cv::Exception normally. If you run your code in a debugger, it will stop at this illegal operation, and you can see the whole call stack with all of your codes and variables at the time of the error.
I´ve got this problem by using OpenCV with WebCam. The problem in my case is that the program is trying to read an image when the Cam hasn't been initialized.
my error code:
// open camera
capture.open(0);
while (1){
//store image to matrix // here is the bug
capture.read(cameraFeed);
The solution
// open camera
capture.open(0);
while (1){
//this line makes the program wait for an image
while (!capture.read(cameraFeed));
//store image to matrix
capture.read(cameraFeed);
(sorry about my english)
Thanks

invalid handle exception when debugging

I have following message while I am in debug mode in Visual studio 2010 Unhandled exception at 0x76c5f9e2 in test.exe: 0xC0000008: An invalid handle was specified.When I run this in regular mode I don't get this error.
The debugger stops in close.c at this line
CloseHandle( (HANDLE)_get_osfhandle(fh) ) )
Does anyone have any sugestion how such error could be avoided?
Don't handle this exception, but avoid the situation were you pass an invalid handle!
Edit:
In debug mode, the code gives you a hint that something is wrong. So you should fix that error instead of handle the exception afterwords!
I just had the same problem.
I checked for stack corruption, corruption of the handle etc. Eventually I discovered that I shouldn't have been calling CloseHandle()!
This was a handle returned by FindFirstFile(), the correct disposal function is FindClose(), not CloseHandle().