Skip to content

Executable does not terminate when using OpenCV and ITK together #1580

Closed

Description

@ntatsisk

Description

Hello,

I am working on a project that uses both ITK and OpenCV. My code runs normally without any errors. However, it does not terminate even if the main() function reaches the return statement (I have to manually force-stop it each time). I noticed that this problem occurs when in a specific combination of functions is used from each library (see working example). The problem appears both in Release and Debug mode. In addition, I can see in Visual Studio that the memory is not released. Thanks!

Steps to Reproduce

I can reproduce the issue both running the executable from inside the Visual Studio and the command line. A minimal working example showing the issue is the following:

#include <string>
#include "itkImageFileWriter.h"
#include <opencv2/opencv.hpp>


int main( int argc, char* argv[] ) {
  typedef itk::Image< float, 2u > ImageType;  
  typedef itk::ImageFileWriter< ImageType > ResultImageWriterType;
  typedef ResultImageWriterType::Pointer ResultImageWriterPointer;

  const std::string filename = "test.jpg";

  // If you comment any of these two lines, the problem disappears
  cv::Mat frame0 = cv::imread(filename, cv::IMREAD_GRAYSCALE);
  ResultImageWriterPointer outputImageWriter = ResultImageWriterType::New();

  return 0;
}

Expected behavior

The executable should stop running.

Actual behavior

The executable seems to continue running.

Reproducibility

It occurs every time.

Versions

ITK 5.0.1
OpenCV 4.2.0

Environment

CMake 3.16.2
Visual Studio 2019 with MSVC compiler x64
Windows 10 - 64bit

Additional Information

The CMakeLists.txt:

cmake_minimum_required( VERSION 3.16.2)

project( bugITKOpenCV )
set(CMAKE_CXX_STANDARD 11)

list(APPEND CMAKE_PREFIX_PATH "C:/opencv/build" "C:/itk5")

# ITK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
 
# OPENCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

# Add the executables
add_executable(bugTest bugReplicate.cxx)

target_link_libraries(bugTest ${ITK_LIBRARIES})
target_link_libraries(bugTest ${OpenCV_LIBS})

Activity

  1. added
    type:Bug
    on Jan 30, 2020
  2. dzenanz commented on Jan 30, 2020

    @dzenanz
    Member

    I could not reproduce this

    cmake_minimum_required(VERSION 3.10.2)
    
    if(NOT CMAKE_CXX_STANDARD)
      set(CMAKE_CXX_STANDARD 11) # Supported values are ``11``, ``14``, and ``17``.
    endif()
    if(NOT CMAKE_CXX_STANDARD_REQUIRED)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
    endif()
    if(NOT CMAKE_CXX_EXTENSIONS)
      set(CMAKE_CXX_EXTENSIONS OFF)
    endif()
    
    PROJECT(tester CXX)
    
    FIND_PACKAGE(ITK REQUIRED)
    INCLUDE(${ITK_USE_FILE})
    
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    ADD_EXECUTABLE(tester tester.cpp)
    
    TARGET_LINK_LIBRARIES(tester ${ITK_LIBRARIES})
    TARGET_LINK_LIBRARIES(tester ${OpenCV_LIBS})
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT tester)
    #include <string>
    #include "itkImageFileWriter.h"
    #include <opencv2/opencv.hpp>
    
    
    int
    main(int argc, char * argv[])
    {
      typedef itk::Image<float, 2u>           ImageType;
      typedef itk::ImageFileWriter<ImageType> ResultImageWriterType;
      typedef ResultImageWriterType::Pointer  ResultImageWriterPointer;
    
      const std::string filename = "C:/a/100.jpg";
    
      // If you comment any of these two lines, the problem disappears
      cv::Mat                  frame0 = cv::imread(filename, cv::IMREAD_GRAYSCALE);
      ResultImageWriterPointer outputImageWriter = ResultImageWriterType::New();
    
      return 0;
    }

    I used OpenCV3.3 (built using VS2015), recent master of ITK with static libraries, CMake 3.16.3, VS2019 x64, Windows 10, using a debug build. Edit: no problem with RelWithDebInfo build either.

  3. ntatsisk commented on Jan 31, 2020

    @ntatsisk
    Author

    Thank you @dzenanz for taking the time to try reproducing the error. Unfortunately, it persists for me.

    I also tried using the function exit(), which seems that it fixes the issue for this working example but when I try to do the same in my original code, it doesn't fix it. Both cases in Release mode.

    Any hint would be highly appreciated. Otherwise, I will continue force-stopping by program and maybe try on a different computer later on.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include "itkImageFileWriter.h"
    #include "opencv2/opencv.hpp"
    
    
    int main( int argc, char* argv[] ) {
      typedef itk::Image< float, 2u > ImageType;
      typedef itk::ImageFileWriter< ImageType > ResultImageWriterType;
      typedef ResultImageWriterType::Pointer ResultImageWriterPointer;
    
      const std::string filename = "test.jpg";
    
      // If you comment any of these two lines, the problem disappears
      cv::Mat frame0 = cv::imread(filename, cv::IMREAD_GRAYSCALE);
      ResultImageWriterPointer outputImageWriter = ResultImageWriterType::New();
    
      exit(EXIT_SUCCESS);
      return 0;
    
    }
    
    
  4. dzenanz commented on Feb 3, 2020

    @dzenanz
    Member

    Did you solve the issue? What was the problem?

  5. ntatsisk commented on Feb 6, 2020

    @ntatsisk
    Author

    Hey @dzenanz, no I haven't solved the problem. In fact, quite the opposite. I realized that when I run the executable of my original code, it has different behavior when I use the command line than when I use the Visual Studio IDE.

    In the command line, the program cannot even return from the last function called which means that it cannot actually complete the entire execution. This issue does not happen when using the VS (Although, it still does not return from the main() function = I need to force-stop it).

    Since you couldn't reproduce the problem, I closed the issue but maybe that was a mistake :D

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions