Skip to content

buildPyramid crashes on VS 2013 when running in RelWithDebInfo mode. #5564

Description

@panmari

When I call buildPyramid with an image as source and an empty std::vector as destination, I get the following exception (only in RelWithDebInfo):

Unhandled exception at 0x00007FFF1A97512D (msvcp120d.dll) in hole_filling.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

In other modes (Debug, Release), everything works as excepted. This is the stack trace I get:

msvcp120d.dll!00007fff1a97512d()    Unknown
opencv_core300d.dll!std::vector<cv::Mat,std::allocator<cv::Mat> >::_Reallocate(unsigned __int64 _Count) Line 1606   C++
opencv_core300d.dll!std::vector<cv::Mat,std::allocator<cv::Mat> >::_Reserve(unsigned __int64 _Count) Line 1619  C++
opencv_core300d.dll!std::vector<cv::Mat,std::allocator<cv::Mat> >::resize(unsigned __int64 _Newsize) Line 1106  C++
opencv_core300d.dll!cv::_OutputArray::create(int d, const int * sizes, int mtype, int i, bool allowTransposed, int fixedDepthMask) Line 2387    C++
opencv_core300d.dll!cv::_OutputArray::create(int _rows, int _cols, int mtype, int i, bool allowTransposed, int fixedDepthMask) Line 2202    C++
opencv_imgproc300d.dll!cv::buildPyramid(const cv::_InputArray & _src, const cv::_OutputArray & _dst, int maxlevel, int borderType) Line 1507    C++

Activity

  1. StevenPuttemans commented on Oct 23, 2015

    @StevenPuttemans

    Did you build your OpenCV manually? Or used a predownloadable package?

  2. panmari commented on Oct 23, 2015

    @panmari
    Author

    I'm using the repo self built on master (checked out today).

  3. alalek commented on Jan 21, 2016

    @alalek
    Member

    Are you able to reproduce this crash with some of OpenCV tests? Or provide some sample code (and build steps description) to reproduce this problem.

  4. panmari commented on Jan 21, 2016

    @panmari
    Author

    Sorry, I'm not really involved in this anymore. If nobody else ever encountered this issue, it would probably be best to close it.

  5. jcar87 commented on Mar 11, 2016

    @jcar87

    I'm having the same problem. I think I'm fairly certain what's causing it and how to fix it.
    This is the perfect cmake / vc++ debug runtime storm.

    When building OpenCV with CMake, you will most likely do it in either of the two configurations: Release and Debug, or possibly both. On Windows, both can coexist on the same directory because the filenames are different ('d' suffix for debug).

    When importing the targets into your cmake project using find_package, this also works well because OpenCVModules-debug.cmake and OpenCVModules-release.cmake include the necessary information to handle both cases.

    Your own cmake project, by default, typically includes four different configurations: Release, Debug, RelWithDebInfo and MinSizeRel. But when you link your own targets against any of the imported opencv ones, cmake only has information for Release and Debug.

    When you are building your own project with 'RelWithDebInfo', CMake is faced with the choice of which configuration to use, and for some reason, it chooses the 'debug' one instead of the release one. The release one is the one that should be used for RelWithDebInfo because release AND RelWithDefInfo are linked against the multithreaded-DLL runtime library (/MD flag), and debug is /MDd (multithreaded DLL debug).

    No need to go into much more detail, but the reason it crashes is because your own code will use /MD and the debug OpenCV uses /MDd, which contain different iterator implementations (you can read more about the visual c++ "iterator debug level" ) . That's why it's crashing in a function that uses an input array, because internally, somewhere, an std iterator is being used. This is why some OpenCV functions will work fine in RelWithDebInfo even when linked against debug OpenCV, and some others will crash right away.

    Back to CMake:
    there is a setting that you can use in your own project, to tell cmake which imported configuration to use for each of your own configuration types.
    https://cmake.org/cmake/help/v3.0/prop_tgt/IMPORTED_CONFIGURATIONS.html

    A possible solution would be, in your own project, to define it:
    set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)

    this is telling cmake that YOUR "RelWithDefInfo" configuration must use the "Release" configuration for imported targets. find_package(OpenCV) will import all the opencv targets.

    The problem is that this setting does not only affect opencv, but ANY other imported targets. I myself had have problems with this: if I set this variable, I lose some of cmake's Qt functionality when building in RelWithDebInfo.
    This is described here: http://public.kitware.com/Bug/view.php?id=15451&nbn=1

    So another solution would be to do exactly what Qt did to fix this very same problem:
    https://codereview.qt-project.org/#/c/48671/

    They basically say that if you load the 'release' configuration first, this configuration will be used by default for any configuration that does not exactly match debug. After some digging, I found where OpenCV does this, in OpenCVModules.cmake:

    get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
    file(GLOB CONFIG_FILES "${_DIR}/OpenCVModules-*.cmake")

    and then include each file found. So, alphabetically 'debug' comes before 'release' and that's why debug is loaded first.

    Solution? OpenCVModules.cmake should always include release first, then any other OpenCVModules-config.cmake file found.

    Summary:

    • If you built OpenCV with RelWithDebInfo configuration, your own project needs to use /MDd (multithreaded DLL debug runtime), otherwise it can crash when the operations involve iterators.
    • If you are using CMake to generate your project, and import OpenCV targets by pointing find_package to OpenCVconfig.cmake, currently only Release and Debug will work okay, but the other two default configurations will incorrectly use 'debug', which can cause the aforementioned crashes.
    • One possible solution is so set set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE) in your project, but this can interfere with other libraries which provide imported targets.
    • A more permanent solution would be for OpenCV's built script to generate an OpenCVModules.cmake file which includes 'release' ahead of any other configuration found.

    As for this bug report, it doesn't only affect imgproc.

    I hope this is detailed enough !

  6. alalek commented on Mar 11, 2016

    @alalek
    Member

    @jcar87 Thanks for detailed problem description!

    Another workarounds:

    • try to build OpenCV for RelWithDebInfo configuration additionally
    • remove unused debug configuration by removal of OpenCVModules-debug.cmake

    BTW, OpenCVModules.cmake is fully generated by CMake (auto-generated file), not sure we should modify it.

  7. jcar87 commented on Mar 11, 2016

    @jcar87

    The only two configurations on the OpenCV cmake script are "Debug" and "Release", not RelWithDebInfo. Additionally, unless the cmake scripts are set up to produce different filename for the libraries, I presume they would clash with the "Release" ones. Right now you can conveniently "install" both Debug and Release on the same directory.

    If that file is fully generated by CMake then it may be worth a try checking with them. Or, even with Qt. The CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO variable does fix the problem for OpenCV but overrides Qt's own settings, so it won't play nice when both Qt and OpenCV are used in the same project. Will continue investigating ...

  8. dtmoodie commented on Aug 3, 2016

    @dtmoodie
    Contributor

    You can also copy your OpenCVModules-Release.cmake file to OpenCVModules-RelWithDebInfo.cmake, then replace all instances of RELEASE with RELWITHDEBINFO in the file.

  9. bjornpiltz commented on Oct 27, 2016

    @bjornpiltz

    Thanks jcar87! That is really helpful. I would add some thoughts, though.

    1. As a user, include the line "SET_TARGET_PROPERTIES(${OpenCV_LIBS} PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO RELEASE)". This fixes the problem locally without polluting Qt and other projects.
    2. As a user, don't add "/MDd" to your local RelWithDebInfo configuration as this defeats the purpose of that configuration.
    3. OpenCV developers: Please integrate the fix mentioned as "generate an OpenCVModules.cmake file which includes 'release' ahead of any other configuration found.". It should be almost trivial to implement and is is a much saner default than linking to the debug version. Even if you don't want to support RelWithDebInfo builds (Please consider supporting RelWithDebInfo #6496), you should at least aspire to function correctly for projects using that setting.
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