Skip to content

cv::imwrite() always crashes when accessing the params vector (Windows 8.1 x86) #5227

Closed

Description

@prospo

When writing an image to disk (cv:imwrite) using an encoding which accesses the Vector<int>& params the code will always crash during runtime with an "Access violation". I am able to reproduce with encodings JPG and PNG. If an encoding is used which doesn't use the params vector the problem doesn't exist (e.g. BMP).

I am running Windows 8.1 Pro with Visual Studio 2013 and OpenCV from git (master). If needed I have all build settings and files stored and can rebuild/test upon request.

Code to reproduce:
std::string filename = "file.png";
cv::Mat image cv::Mat(600, 800, CV_8U);
cv::imwrite(filename.c_str(), image);

Second code to reproduce:
std::string filename = "file.jpg";
cv::Mat image cv::Mat(600, 800, CV_8U);
std::vector<int> my_params;
my_params.push_back(CV_IMWRITE_JPEG_QUALITY);
my_params.push_back(80);
cv::imwrite(filename.c_str(), image, my_params);

A workaround is to just use a different params vector which is cleanly created inside the imwrite_ static function. This workaround FORGETS and silently IGNORES any given parameters and let's the functions fall back to any default opencv parameters for the specific image encoding.

Open modules\imgcodecs\src\loadsave.cpp change function:
static bool imwrite_( const String& filename, const Mat& image, const std::vector<int>& params, bool flipv )
Find the following line:
bool code = encoder->write(*pimage, params);
Replace with:
//bool code = encoder->write(*pimage, params);
bool code = encoder->write(*pimage, {});

Activity

  1. ilya-lavrenov commented on Aug 20, 2015

    @ilya-lavrenov
    Contributor

    Could you create merge-request with proposed changes and regression tests?

  2. prospo commented on Aug 20, 2015

    @prospo
    Author

    @ilya-lavrenov the workaround is really not suitable for a merge-request as it just forgets the params given. More research is needed to determine the platform specific cause of this bug.

  3. MaverickTse commented on Oct 26, 2015

    @MaverickTse

    Just to report that an up-to-date build of OpenCV using MSVC or INTEL compiler(32bit build), No CUDA, No OpenEXR, No contrib, CAN output PNG.

    It seems fixed. Confirmed with 64bit VC2013 build of OpenCV with CUDA and OpenEXR.

  4. Dikay900 commented on Dec 23, 2015

    @Dikay900
    Contributor

    Can confirm this is not an issue anymore with latest sources. Using the params exactly like the reproducable code.

  5. alalek commented on Dec 23, 2015

    @alalek
    Member

    Thanks for checks!

  6. kikaxa commented on Dec 14, 2016

    @kikaxa
    Contributor

    can we reopen this?
    i still get this same crash with msvc2010, opencv 3.1

  7. alalek commented on Dec 14, 2016

    @alalek
    Member

    @kikaxa Open a new issue with complete reproducer code, crash stack trace and OpenCV build configuration.
    There are many cases where imwrite is used, and it works without crashes.

  8. kikaxa commented on Dec 14, 2016

    @kikaxa
    Contributor

    after some painfull debugging, it might be the app/compiler/crt that is bugged.
    it crashed with mixed debug(app)/release(opencv) modes, and a lot of other things were broken, too.
    for now it works in regular build, i'll open the new bug if crash reappears.

  9. PkLab commented on Dec 15, 2016

    @PkLab
    Contributor

    I've same issue (Win7, VS2013, OCV 3.1.0/x64 home build)

    it crashed with mixed debug(app)/release(opencv) modes

    Both cases debug(app)/debug(opencv) and release(app)/release(opencv) work fine

    I think that a new issue should be opened

  10. alalek commented on Dec 15, 2016

    @alalek
    Member

    new issue should be opened

    Crashes in mixed debug/release mode can't be solved in OpenCV - it is MSVS runtime specific.

  11. PkLab commented on Dec 16, 2016

    @PkLab
    Contributor

    Do you want to say that, OpenCV Release build can't be used with end user application Debug build due to MSVS runtime specific ?

    any investigation might be useful also around other functions and maybe a workaround or a compiler switch exists.

    ...looking at imwrite here the params isn't correctly assigned/initialized/linked in Debug build

    Adding here a simple

    std::cout << "params.size()=" << params.size() << std::endl;
    

    The following test prints out params.size()=184467440736......

    imwrite("Test.jpg", mat,params);
    

    The following test prints out params.size()=32

        params.push_back(0);
        params.push_back(0);
        imwrite("Test.jpg", mat,params);
    
  12. alalek commented on Dec 16, 2016

    @alalek
    Member

    ABI is different in debug/release builds for most C++/STL structures, so you can't used them in mixed mode.

    Please take a look on this thread in QT project:
    http://www.qtcentre.org/threads/54332-Mixing-Debug-and-Release-binaries

    The only thing that probably can be done in OpenCV to help users - it is to prevent successful builds (raise some error).

  13. PkLab commented on Dec 16, 2016

    @PkLab
    Contributor

    @alalek thank you very much for your useful answer. quoting the user from the referring thread

    I'm wondering that some software i use do not need to ship the debug dlls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions