Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?



  • Hi,

    I have built OPENCV v4.1.1 from source for Qt. I have linked the necessary libraries and added the include path in the .pro file as follows:

    INCLUDEPATH += "C:\opencv4-build-x64\install\include"
    
    LIBS += -L"C:\opencv4-build-x64\install\x64\mingw\bin" -lopencv_core411 \
    -lopencv_highgui411 \
    -lopencv_imgproc411 \
    -lopencv_imgcodecs411
    

    The libraries are .dll files. Now in my source code, I have something like this:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/imgcodecs/imgcodecs.hpp>
    
    int main(){
    
    Mat img = imread("SamplePath.tiff", IMREAD_GRAYSCALE);
    
    }
    

    The program builds fine but when I try to run it, it crashes immediately. If I comment out all the imread() and imwrite() functions, then it runs perfectly.

    Any ideas ? Thanks.


  • Lifetime Qt Champion

    Hi,

    "SamplePath.tiff" is a relative path. Is that file in the same folder as your application ?



  • @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    Hi,

    "SamplePath.tiff" is a relative path. Is that file in the same folder as your application ?

    Yes. The program doesn't even open though. And it does the same for imwrite(). So I doubt the path is the problem...


  • Lifetime Qt Champion

    Did you also copy the OpenCV dlls in the same folder as your executable ?



  • @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    Did you also copy the OpenCV dlls in the same folder as your executable ?

    No, the path to the DLLs is in my systems environment path.


  • Lifetime Qt Champion

    Then you should use a tool like Dependency Walker to check whether you have dependencies not found.



  • @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    Then you should use a tool like Dependency Walker to check whether you have dependencies not found.

    Ok thanks, I'll give it a try...



  • @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    Then you should use a tool like Dependency Walker to check whether you have dependencies not found.

    Ok thanks, I'll give it a try...

    Hi, dependency walker did not help. It listed some missing dependencies however they have nothing to do with OpenCV and they are still shown as missing even when the program runs fine (when I comment out the imread() function)...


  • Lifetime Qt Champion

    Did you try to start your application with the debugger ?
    What stack trace do you get ?


  • Moderators

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    LIBS += -L"C:\opencv4-build-x64\install\x64\mingw\bin" -lopencv_core411
    -lopencv_highgui411
    -lopencv_imgproc411
    -lopencv_imgcodecs411

    You are linking to the Release DLLs. Are you building your app in Release mode or Debug mode? (A Debug mode app requires Debug DLLs, like opencv_core411d.dll)


  • Banned

    This post is deleted!


  • @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    Did you try to start your application with the debugger ?
    What stack trace do you get ?

    I did, the debugger just ends immediately with no output that I can see.

    @jksh said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    LIBS += -L"C:\opencv4-build-x64\install\x64\mingw\bin" -lopencv_core411
    -lopencv_highgui411
    -lopencv_imgproc411
    -lopencv_imgcodecs411

    You are linking to the Release DLLs. Are you building your app in Release mode or Debug mode? (A Debug mode app requires Debug DLLs, like opencv_core411d.dll)

    I am building in Release mode.

    @jhoney said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    "SamplePath.tiff" is a relative path. Is that file in the same folder as your application ?

    Yes it is, but as stated before this is not the issue as the program does not even startup.

    • What is very strange is that running the program from inside Qt I get the error: "The program has unexpectedly finished. The process was ended forcefully".

    • But running the executable directly on my PC, it starts up fine.

    • When running the program on another machine (with the required .DLLs copied into the release folder) I get this error:
      0_1566406119377_Capture.PNG


  • Lifetime Qt Champion

    This usually is the sign of a mix of debug and release libraries.



  • @sgaist said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    This usually is the sign of a mix of debug and release libraries.

    I'm not sure how that's possible since I built OpenCV for release mode and the path to all the DLLs is the same.


  • Moderators

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    • What is very strange is that running the program from inside Qt I get the error: "The program has unexpectedly finished. The process was ended forcefully".

    • But running the executable directly on my PC, it starts up fine.

    • When running the program on another machine (with the required .DLLs copied into the release folder) I get this error:
      0_1566406119377_Capture.PNG

    • When you run your .exe directly on your PC, it loads the correct DLLs.
    • When you run your .exe from Qt Creator, it loads the wrong DLLs.
    • You have copied the wrong DLLs to the other machine (OR, that other machine has incompatible DLLs in its PATH)

    You should:

    1. Make sure your program does not quit (for example, add system("pause"); at the end of main()).
    2. Build your .exe and run it directly on your PC.
    3. While it is still running, use ListDLLs to print every single DLL that is loaded: https://docs.microsoft.com/en-us/sysinternals/downloads/listdlls

    Pay attention to the absolute paths of every single DLL that's reported by ListDLLs. What do you notice?

    If you can't find any clues, repeat the process on your other machine. Run ListDLLs while the "Entry Point Not Found" error dialog is open. Compare the lists from your 2 machines.

    No, the path to the DLLs is in my systems environment path.

    I don't recommend adding DLLs to your global PATH. This is a common cause of DLL Hell.



  • @jksh said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    • When you run your .exe directly on your PC, it loads the correct DLLs.
    • When you run your .exe from Qt Creator, it loads the wrong DLLs.
    • You have copied the wrong DLLs to the other machine (OR, that other machine has incompatible DLLs in its PATH)

    You should:

    1. Make sure your program does not quit (for example, add system("pause"); at the end of main()).
    2. Build your .exe and run it directly on your PC.
    3. While it is still running, use ListDLLs to print every single DLL that is loaded: https://docs.microsoft.com/en-us/sysinternals/downloads/listdlls

    Pay attention to the absolute paths of every single DLL that's reported by ListDLLs. What do you notice?

    If you can't find any clues, repeat the process on your other machine. Run ListDLLs while the "Entry Point Not Found" error dialog is open. Compare the lists from your 2 machines.

    No, the path to the DLLs is in my systems environment path.

    I don't recommend adding DLLs to your global PATH. This is a common cause of DLL Hell.

    Hi, thanks for your help. So I ran listDLLs on both machines as you suggested and found that there were 3 discrepancies related to DLLs that I use:

    • mingw64\bin\libgcc_s_seh-1.dll
    • mingw64\bin\libwinpthread-1.dll
    • mingw64\bin\libstdc++-6.dll

    I found that these DLLs are located in both the Qt folder and minGW compiler folder. I replaced the Qt versions of the DLLs with the ones from the minGW folder and then the program ran fine on the other machine.

    So now the .exe runs fine directly on my PC and the other machine, HOWEVER it is still instantly crashing when running it from Qt directly AS LONG AS THE IMREAD() AND IMWRITE() FUNCTIONS ARE THERE. If I comment them out it runs fine from inside Qt.

    What do you think is the issue with Qt and OpenCV ? Maybe it's to do with the options I set when I built OpenCV from source with CMAKE ? It really doesn't make sense to me that the executable runs fine yet from inside Qt it crashes ? I've never had that happen before.

    I always use this article as guidance: How_to_setup_Qt_and_openCV_on_Windows where it says to add OpenCV and minGW to the environment path. I've never had issues before, only now with OpenCV v4.1.1.


  • Moderators

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    I found that these DLLs are located in both the Qt folder and minGW compiler folder. I replaced the Qt versions of the DLLs with the ones from the minGW folder and then the program ran fine on the other machine.

    You have 2 different versions/variants of MinGW on your machine that are incompatible with each other.

    You must make sure that both Qt and OpenCV are both compiled with the same version/variant of MinGW.

    It really doesn't make sense to me that the executable runs fine yet from inside Qt it crashes ? I've never had that happen before.

    Like I said before, your system is loading different DLLs in those 2 different cases.

    I always use this article as guidance: How_to_setup_Qt_and_openCV_on_Windows where it says to add OpenCV and minGW to the environment path. I've never had issues before, only now with OpenCV v4.1.1.

    You did not have incompatible versions of MinGW before. Now you do.

    Questions:

    1. What version(s) of Qt do you have?
    2. How did you install Qt?
    3. What version(s) on MinGW do you have?
    4. How did you install MinGW?
    5. Which version of MinGW did you use to build OpenCV?


  • @jksh said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    You have 2 different versions/variants of MinGW on your machine that are incompatible with each other.

    I have a 32bit minGW compiler and a 64bit minGW compiler.

    You must make sure that both Qt and OpenCV are both compiled with the same version/variant of MinGW.

    Like I said before, your system is loading different DLLs in those 2 different cases.

    Both are using the 64bit compiler.

    You did not have incompatible versions of MinGW before. Now you do.

    No the environment was exactly the same before. And my other projects with OpenCV V2 and V3 work fine.

    Questions:

    1. What version(s) of Qt do you have?

    5.10.1 for 32bit Kit and 5.5.0 for 64bit kit.

    1. How did you install Qt?

    Normally through the installer.

    1. What version(s) on MinGW do you have?

    5.3.0 32bit and I'm not sure of the 64bit version.

    1. How did you install MinGW?

    32bit came with Qt. 64bit I downloaded.

    1. Which version of MinGW did you use to build OpenCV?

    64bit.

    I'm beginning to think that the problem is something to do with building OPENCV V4.1.1.


  • Moderators

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    So I ran listDLLs on both machines as you suggested and found that there were 3 discrepancies related to DLLs that I use:

    • mingw64\bin\libgcc_s_seh-1.dll
    • mingw64\bin\libwinpthread-1.dll
    • mingw64\bin\libstdc++-6.dll

    I found that these DLLs are located in both the Qt folder and minGW compiler folder. I replaced the Qt versions of the DLLs with the ones from the minGW folder and then the program ran fine on the other machine.

    The above shows that the DLLs that were previously in the Qt folder are incompatible with the DLLs in the MinGW compiler folder.

    Could your build of OpenCV 4.1.1 be similarly contaminated?

    I always use this article as guidance: How_to_setup_Qt_and_openCV_on_Windows where it says to add OpenCV and minGW to the environment path. I've never had issues before, only now with OpenCV v4.1.1.

    ...

    No the environment was exactly the same before. And my other projects with OpenCV V2 and V3 work fine.

    Does that mean you have multiple versions of OpenCV in your PATH at the same time?

    Did you use the same version of MinGW to build all your versions of OpenCV?

    32bit came with Qt. 64bit I downloaded.

    Qt 5.12 and Qt 5.13 come pre-built for MinGW 7.3.0 64-bit.

    What do you think of using one of those? That way, you can use an official build of Qt and its compiler to build your copy of OpenCV 4.1.1; you don't need a custom-built copy of Qt? (Note: Qt 5.5 reached end-of-life in March 2018)



  • @jksh said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    The above shows that the DLLs that were previously in the Qt folder are incompatible with the DLLs in the MinGW compiler folder.

    Could your build of OpenCV 4.1.1 be similarly contaminated?

    In what sense, because I specified the correct paths to the compiler and Qt folders...

    Does that mean you have multiple versions of OpenCV in your PATH at the same time?

    Yes, although I have used this setup across multiple projects using different versions of opencv and I didn't run into any problems.

    Did you use the same version of MinGW to build all your versions of OpenCV?

    No, I have 32bit builds of OpenCV and 64bit builds.

    Qt 5.12 and Qt 5.13 come pre-built for MinGW 7.3.0 64-bit.

    What do you think of using one of those? That way, you can use an official build of Qt and its compiler to build your copy of OpenCV 4.1.1; you don't need a custom-built copy of Qt? (Note: Qt 5.5 reached end-of-life in March 2018)

    That was exactly what I was looking for. Are you sure the 64bit compiler is available now with Qt ? And if so, when was it added because I did the custom setup like 6 months ago specifically because Qt only shipped with a 32bit minGW compiler ?


  • Moderators

    @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    In what sense

    In the DLL Hell sense.

    Yes, although I have used this setup across multiple projects using different versions of opencv and I didn't run into any problems.

    Can you be 100% sure that that your current problems aren't caused by this setup?

    No, I have 32bit builds of OpenCV and 64bit builds.

    ...and all of those different versions of MinGW and OpenCV are in your PATH...?

    Please do yourself a favour and remove them all from your PATH. You don't need them in the PATH. This type of setup has caused many, many headaches over the years.

    That was exactly what I was looking for. Are you sure the 64bit compiler is available now with Qt ? And if so, when was it added because I did the custom setup like 6 months ago specifically because Qt only shipped with a 32bit minGW compiler ?

    Yes, I'm sure. It was added with Qt 5.12.0, which was released in December 2018.



  • @jksh Thanks, I upgraded both Qt and added the 64bit compiler and now everything is working fine.



  • @r-p-h said in OpenCV 4.1.1 Imread() & Imwrite() Crashes Program ?:

    now everything is working fine.

    so please don't forget to mark your post as solved! Thanks.


Log in to reply