C++ Compilation introduce errors - c++

I have a multi-threaded C++ windows project in Visual Studio 2010 which uses Qt (5.1.1) and OpenCV (2.4.8). When I build it in Debug mode everything runs fine, but when I build it in Release mode the program crashes. Both configurations are almost identical (just in Release I have Multi-threaded DLL /MD and in Debug Multi-threaded Debug DLL /MDd), I disabled optimizations for the Release, and even enabled debug to catch the error. What's most weird is that the same piece of code that crashes, runs just fine in another console project.
The error is internal to OpenCV's code, it's not related to my code, my code is just:
void MyProject::findEllipses(QImage &frame, vector<RotatedRect> &ellipses)
{
Mat image = Mat(frame.height(), frame.width(), CV_8UC4, frame.scanLine(0));
cvtColor(image, image, CV_RGB2GRAY);
GaussianBlur(image, image, Size(3, 3), 0, 0, 4);
threshold(image, image, treshVal, 255, THRESH_BINARY);
vector<vector<Point> > contours;
Mat contoursImage = image.clone();
findContours(contoursImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0, 0));
// ....Mode Code
As I said in Debug the code runs without a problem, findContours calls _contours.create(total, 1, 0, -1, true); (in line 1720 of OpenCV's contours.cpp) and moves on that method....BUT in Release, when I step into _contours.create(total, 1, 0, -1, true), instead of executing that method, the program jumps to void _OutputArray::clear() const (line 1674 of matrix.cpp) and get's trapped in there, because it calls int k = kind(); in it's first line, which, instead of calling cv::kind(), calls:
int _InputArray::type(int i) const
{
int k = kind();
Which calls againg kind() which again calls type() (instead of kind()as it should), making an infinite recursive loop, bringing a stack overflow.
I tried to make a new Visual Studio 2010 project to see if this is a project creation problem but the problem persisted.
My guess is that the function adresses are wrong in release mode so when it tries to call create(), it instead calls another address, making a mess in the stack, but that's just my guess. At first I will blame OpenCV release dlls, but, as I said, in another console project that only runs that particular code, the code runs fine in bot build modes. I don't see how my other threads and code can trigger this behavior, since the error goes down to a wrong function call in OpenCV.
This is way beyond my knowledge and I have no idea how to fix this, I appreciate all the help I can get on this problem, because I already exausted all my ideas to fix it....
EDIT :
I made a small project so anyone can see what's happening.
The files are in:
https://app.box.com/s/3owljl44emv57erinrf8
In order to run it, you have to have OpenCV 2.4.8 and Qt 5.1.1 and configure the Visual Studio 2010 project to get the include files from the right places. In the resources folder there is a image that will be loaded, and in the SaraVisualControl.cpp line 20, you have to place the right path to the image, sorry I didnt made this automatic, but I was in a hurry to pack this in a small project. Any other questions about how to run it, please let me know.
EDIT 2
I found this old thread: http://code.opencv.org/issues/2218 the user appears to have the same problem as I have, and just running CMake with OPENCV_CAN_BREAK_BINARY_COMPATIBILITY turned off appears to solve it. But this option is no longer present in the new versions of OpenCV, as stated here http://code.opencv.org/issues/2358. Does anyone knows the implications of this and how it may be related?

I encountered similar symptoms in the past, when mixing VC runtime versions. If you're working in VC2010 and OpenCV 2.4.8 was built on 2012 or even 2013, your ABI does not match. E.g., the layout in memory of std::vector is different, maybe the order of some methods in a vtable somewhere changed, etc.
An easy way to test this is to inspect the 'Modules' window while debugging and look for runtime dll's of a later version. Yours is msvcr100d.dll, if you see msvcr110d or 120d - this might be the source.
(assuming both you and OpenCV link dynamically to the CRT - which I cannot tell).

The debug and release are different in memory allocations. Usually in Debug mode that allocs are bigger.
May be you abuse some openCV call, but you don't aware of it.
I would do next actions:
Validate that the frame is ok. May be it is too big, or width or height are not initiated;
Validate that after next line the image var is OK
Mat image = Mat(frame.height(), frame.width(), CV_8UC4, rame.scanLine(0));
Is it OK to call cvtColor while the 1st and 2nd param are the same (may be you abuse the CV here)
Is it OK to call GaussianBlur while the 1st and 2nd param are the same? Try to put different 2nd param
The same is with threshold function call
Check that after image.clone call, the countoursImage is a valid matrix.

I'm guessing: In addition to checking that the libraries are all correct themselves, check that you specify all libraries in the same order (in all builds). Otherwise, the linker might bind function names to different implementations during the final linker stage.

I had the same issue.I was using Qt 5.1.1 and opencv248 and my application was crashing after returning from findcontours. Turns out I was using the vc10 libs in the pro file and the corresponding dlls whereas the Qt compiler being used was msvc2013. Switching to the vc12 folder in opencv 248 solved the problem for me.

Have you checked your opencv libraries for Release version.
Opencv has different set of libraries for Release and Debug version. if you have accedently included debug libraries in the release mode. your code will compile with no error but will throw excecption when executed.
To check your libraries goto
Project->Properties->Configuration Properties->linker->Input->Additional Dependencies.
make sure none of your Opencv libraries ends with "d" for release mode.
library for Release : 'opencv_core248.lib'
library for Debug : 'opencv_core248d.lib'
notice the d at the end for debug mode.
I hope this would be helpful.

Related

Error in opencv function in .exe file but not in debug nor release

One more time, I want to ask for your help after long and unfruitful researches.
I have developped an app using OpenCV which works very well is Visual Studio in debug and release mode.
I tried to deploy it, but i get a fatal error when I start the .exe. By using a log file and doing tests, I figured out the problem was in function cv::resize in the following code :
Mat logo = imread("lena.png");
cv::resize(logo, logo, Size(), 0.55, 0.55, INTER_CUBIC);
I found very weird that i can call cv::imread (and cv::Mat) without throwing exception but not resize.
If I try to run the .exe created by the compilation in release mode, i get the following errod code : 0xc000007b.
I read it could be a problem of compatibility between 32/64-bits dll, without being able to solve it. I try to build a 64-bits appplication. The path for opencv lib is define like this : C:\opencv\build\x64\vc14\lib
Thank you for your help,
Valentin B

dlib object detection always return zero results on iOS

I have been using dlib object detection successfully on mac. Now I want to use it in an iOS app. However, after spending countless hours, dlib object detector always returns zero rectangles.
ifstream fin(dataDir + "/object_detector.svm", ios::binary);
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<6> > image_scanner_type;
dlib::object_detector<image_scanner_type> detector;
dlib::deserialize(detector, fin);
vector<dlib::rectangle> dets = detector(dlibImage);
To make sure it’s not due to a different image, I am using exact same image for which detector returns 1 hit on mac. I have also printed uchar from part of the image in both mac and iOS, and it’s returning same values. So image data is exactly the same.
Probably dlib library is not built correctly for iOS. I have tried multiple approaches for this. From /example/build dir, below command was invoked.
cmake -G Xcode ..
cmake --build . --config Release
It generated dlib.xcodeproj project in dlib_build dir. I opened the project in xcode, changed architecture to iOS (armv7, arm64) and rebuild the library. This library was linked to my project. I got zero results with this approach. dlib was built in debug mode; I did not get any assertion errors.
Second approach tried was to use dlib/all/source.app in my project. I used all the proprocessing flags that are used by cmake or dlib.xcodeproj project. No errors, but still no matches.
I have compared build settings of my xcode project with examples.xcodeproj generated by cmake and it’s same. Also checked the xcode project from https://github.com/zweigraf/face-landmarking-ios, but no help.
Strange thing is detector takes couple of seconds to process and comes back with zero matches. So it’s doing something. I wish there was a debug logging that I can turn on for the detector.
I am out of ideas. Will appreciate if anyone can help. dlib is a wonderful library, I just wish it would have been easier to work with on iOS.
dlib is working fine on iOS too. Kicking myself for it, but I mixed up the detector instances. The detector on which I called below line was not used for object detection.
dlib::deserialize(detector, fin);
I was just using an empty detector instance, and it was returning 0 detections. By empty detector, I mean it was defined but deserialize method was not invoked. It would have been nice if dlib returned an error or warning, if a detector not loaded with object_detector.svm file is used for detection.
I have observed the same behavior with shape detector too. If sp.dat is not loaded, it silently reports 0 parts detected. Posting this as answer, in case someone else also makes such a silly mistake.

Failed to use imwrite() in Qt Creator in Debug mode

I used imwrite() function in Qt Creator. I failed to run the code in Debug mode,while successfully in Release mode.
I wrote a test program.The followings is what I have done:
First to use OpenCV, I added the following code in .pro:
INCLUDEPATH+=D:\Work_Software\OpenCV3.1\opencv\build\include
LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*.lib
Then I added a Push Button. The slot function is:
void MainWindow::on_pushButton_clicked()
{
Mat img;
img=imread("F:\\My_Desktop\\foot1.jpg",0);
imwrite("F:\\My_Desktop\\result.jpg",img);
namedWindow("test");
imshow("test",img);
waitKey(0);
}
Finally, in Release mode, I can successfully read and write the image. However, failed to write in Debug mode. The error information is:
UPDATE
imread works in debug mode, for example, I change the slot function into:
void MainWindow::on_pushButton_clicked()
{
Mat img;
img=imread("F:\\My_Desktop\\foot1.jpg",0);
namedWindow("test");
imshow("test",img);
waitKey(0);
imwrite("F:\\My_Desktop\\result.jpg",img);
}
I can successfully load and imshow the image in Debug mode, but when I closed the windows, the same error happened.
The content of lib folder:
I have just seen a problem like mine similar problem , but it could not fix mine.
It happens because you included all library using *.lib command. In debug mode if you links with release libraries it fails. It works in release mode cause it links with release libs as it comes first due to string sort. See the image
Here 2411d.lib stands for debug library and 2411.lib stands for release library. I faced this issue and fixed separate linking in debug & release mode. You can either make 2 folders of debug and release libraries or you can mention the library names instead of *.lib.
[change the version for you]
Debug link: LIBS+=D:\Work_Software\OpenCV2.411\opencv\build\x64\vc12\lib\*‌​d.lib
Release link: LIBS+=D:\Work_Software\OpenCV2.411\opencv\build\x64\vc12\lib\*‌​2411.lib
OR
To separate folder See the images:
Folder stucture:
Debug Library folder:
Release Library folder:
UPDATE
If opencv is not built with qt properly please follow link
OpenCV dll and lib files are differ in terms of CPU architecture (32-64 bit) and Debug-Release mode. if you switch to Debug mode you must use dll and lib files for Debug mode (depends on CPU architecture).

The SDL library I built from source crashes!

I've successfully built SDL from source using bcc 5.5.1 but any SDL test application using it crashes right away at startup. I'm looking for some help and/or guidance on how to resolve this issue.
Just to fill in some info, SDL-1.2.14 was used. The project's compiled as a dll with multithreading enabled and linked to C runtime dynamically. I've also rebuilt it with debugging info. When I step through with a debugger up to the point of crash it seems to be coming from redirect_stdout in the sdlmain. If I remove sdlmain.lib and use the source file sdl_win32_main.c directly in the SDL test project then that doesn't crash anymore. Instead it just crashes later on at SDL_Init routine.
I've already checked the calling conventions used and they all seem to match up -- everything is using cdecl. I've also checked and made sure the compiled sdl.dll and the test application was using the same dynamic c runtime instead of statically linked.
The SDL wiki under Borland section mentions to use the -b to make sure enum's are same size as int but that option is enabled by the compiler by default unless explicitly turned off. I did rebuild SDL with that compiler/linker switch just to be sure though.
When it crashes, it's always a access violation in trying to write to some address(c000005). Like for example during a typical SDL init attempt like this:
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
After the call into SDL_Init(), control doesn't flow back into the test application. Instead it crashes somewhere bizarre like somewhere in ntdll.dll with something having to do with NTDLL.RtlEnterCriticalSection. When I inspect the stack trace at that point I usually get something like this:
:77982269
:0044A04C
:0043F02B
:0043F7C4
:0043EF25
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
SDL_CreateMutex()
SDL_CreateSemaphore(1)
//and it keeps recursing... looks like a stackover? :P
I'm not sure whatelse to try at this point as I'm pretty stumped. If anyone have any suggestions or need me to provide more info please feel free to add it to the comments.
Thanks
Alright, I finally found out what the issue was a couple days ago. The reason for the crash was because the wrong source file was compiled for the given platform.
The project file I used kept compiling SDL_sysmutex.c from threads\generic. The correct SDL_sysmutex.c to use under win32 should have been from threads\win32. I found this out when I was tracing the test programs side by side and the threading modules had different lines of code!
With this little oversight fixed the crashing problems have all but disappeared and all the test demos are running as they should :)

OpenCV cvLoadImage() does not load images in visual studio debugger?

I am trying to work out a simple hello world for OpenCV but am running out of ideas as to why it is not working.
When I compile and run this code:
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage( "myjpeg.jpg" );
cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
cvShowImage("MyJPG", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "MyJPG" );
return 0;
}
I get a grey box about 200x200 instead of the indicated .jpg file. If I use a different jpg I get the same kind of window, and if I put an invalid filename in, I get a very tiny window (expected).
I am using Visual Studio 2008 under Windows 7 Professional.
Most of the sample programs seem to work fine, so I am doubly confused how that code loads the sample jpgs just fine but in the code above it does not work (even tried the sample jpeg).
Update
The executables produced by compiling work fine, however the Visual Studio 2008 debugger loads a null pointer into img every time I try to run the debugger - regardless if the file location is implicit or explicit.
It really seems like there's a problem with the path to myjpeg.jpg since the current directory could be different when you're running under the debugger.
By default, the current directory that the Visual Studio debugger uses is the directory containing the .vcproj file, but you can change it in the project properties (Debugging -> Working Directory).
Are you 100% sure that you pass the absolute path correctly? Try to pass the same path to fopen and see if it also returns NULL. If so, then the path is incorrect.
If you want to see exactly what file is the library trying to open you can use Project Monitor with a filter on myjpeg.jpg.
Which version of OpenCV are you using? I've tried your code on the latest (OpenCV2.0) and it works fine. You can download OpenCV2.0 from here.
If you want the latest build, you can get check it out with SVN from here.
Try to add HAVE_JPEG into preprocessor definitions.
I encountered the same problem. The Debug version does not load the image, but when I compile and link it as a Release it works. I hope this helps