简体   繁体   中英

c++ vtk: hello world fails at run time only in Release

I copied the hello world code into my main c++ hoping to be able to modify it bit by bit in order to slowly create my rendering. I am able to compile it, in both Release and Debug mode. But when I run it, it works in debug and fails in release. I obtain a segmentation fault.

My set up is a cmake project in netbeans. My CMakeList.txt looks like that:

cmake_minimum_required(VERSION 3.6)
project(uvlm)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES
   ... hidden for clarity ...)

include_directories(.
        ${EIGEN_LIBRARY_PATH}
        ${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts
        ${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/Include/
        )

if(CMAKE_BUILD_TYPE MATCHES "Debug")
    set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Debug/bin)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -std=c++14 -D__LINUX__ ")
elseif(CMAKE_BUILD_TYPE MATCHES "Profiling")
    set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Profiling/bin)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -std=c++14 -D__LINUX__ ")
else()
    set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Release/bin)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -std=c++14 -D__LINUX__ ")
endif()

# DEBUG LIBRARIES
find_library(GTE_DEBUG_CXX_LIBRARY NAMES libgtengine.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/lib/Debug)
find_library(USEFULFCTS_DEBUG_CXX_LIBRARY NAMES libusefulfcts.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts/lib/Debug)
# RELEASE LIBRARIES
find_library(GTE_RELEASE_CXX_LIBRARY NAMES libgtengine.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/lib/Release)
find_library(USEFULFCTS_RELEASE_CXX_LIBRARY NAMES libusefulfcts.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts/lib/Release)

FIND_PACKAGE( OpenMP)
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
include_directories(SYSTEM ${OpenMP_INCLUDE_PATH})

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(uvlm ${SOURCE_FILES})
target_link_libraries(uvlm debug ${GTE_DEBUG_CXX_LIBRARY} ${USEFULFCTS_DEBUG_CXX_LIBRARY} ${VTK_LIBRARIES} optimized ${GTE_RELEASE_CXX_LIBRARY} ${USEFULFCTS_RELEASE_CXX_LIBRARY} ${OpenMP_CXX_LIBRARIES} ${VTK_LIBRARIES})

My cmake command line has arguments like so:

${CMAKE} -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${IDE_CC} -DCMAKE_CXX_COMPILER=${IDE_CXX} -DCMAKE_C_FLAGS_RELEASE="-O3  -DEIGEN_NO_DEBUG -march=native -mfpmath=sse -funroll-loops" -DCMAKE_CXX_FLAGS_RELEASE=" -O3 -mfpmath=sse -funroll-loops -DEIGEN_NO_DEBUG " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DEIGEN_LIBRARY_PATH="HIDDENPATH" -DGITREPO_PATH="HIDDENPATH" .

I installed vtk using dnf on Fedora 25: vtk-devel-6.3.0-11.fc25.x86_64

The code I run is below:

void vtkRenderingTest(std::string fileName, bool mbdynInfiniteLoop) 
{
  // This creates a polygonal cylinder model with eight circumferential facets
  // (i.e, in practice an octagonal prism).
  vtkSmartPointer<vtkCylinderSource> cylinder =
    vtkSmartPointer<vtkCylinderSource>::New();
  cylinder->SetResolution(8);

  // The mapper is responsible for pushing the geometry into the graphics library.
  // It may also do color mapping, if scalars or other attributes are defined.
  vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

  // The actor is a grouping mechanism: besides the geometry (mapper), it
  // also has a property, transformation matrix, and/or texture map.
  // Here we set its color and rotate it around the X and Y axes.
  vtkSmartPointer<vtkActor> cylinderActor =
    vtkSmartPointer<vtkActor>::New();
  cylinderActor->SetMapper(cylinderMapper);
  cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
  cylinderActor->RotateX(30.0);
  cylinderActor->RotateY(-45.0);

  // The renderer generates the image
  // which is then displayed on the render window.
  // It can be thought of as a scene to which the actor is added
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(cylinderActor);
  renderer->SetBackground(0.1, 0.2, 0.4);
  // Zoom in a little by accessing the camera and invoking its "Zoom" method.
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(1.5);

  // The render window is the actual GUI window
  // that appears on the computer screen
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(200, 200);
  renderWindow->AddRenderer(renderer);

  // The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // This starts the event loop and as a side effect causes an initial render.
  renderWindowInteractor->Start();

}

int main(int argc, char* argv[])
{
    try {

            // put the file name in the run property of the configurations (Release and Debug)
            bool mbdynInfiniteLoop = false;
//            if (argc > 2) { if (argv[2] == "true") { mbdynInfiniteLoop = true; } }
//            loadsEval(argv[1], mbdynInfiniteLoop);

            vtkRenderingTest("AAAA", true);

        return 0;
    }
    catch (std::string &e) {
        std::cout << "############### Error #####################\n\n";
        std::cout << e << std::endl;
        std::cout << "\n###########################################\n\n";
    }
    catch (hyd::HydExceptions &e) {
        std::cout << "############### Error #####################\n\n";
        std::cout << e.errMessage << std::endl;
        std::cout << "\n###########################################\n\n";

    }
}

Remember that it works perfectly in Debug mode.

The conflicting line is the start of the GUI, namely renderWindowInteractor->Start();

I tried to copy the debug cmake commands to the release one but it didn't work. A sort of hidden behaviour is acting here that I cannot find out!

EDIT: Here is the output you suggested to have a look at. It seems a VTK issue? `

==3082== Process terminating with default action of signal 11 (SIGSEGV)
==3082==  Access not within mapped region at address 0x0
==3082==    at 0x1FAD435F: rawmemchr (in /usr/lib64/libc-2.24.so)
==3082==    by 0x1FABC831: _IO_str_init_static_internal (in /usr/lib64/libc-2.24.so)
==3082==    by 0x1FAAE7E6: vsscanf (in /usr/lib64/libc-2.24.so)
==3082==    by 0x1FAA8A26: sscanf (in /usr/lib64/libc-2.24.so)
==3082==    by 0x143292B3: vtkOpenGLExtensionManager::ReadOpenGLExtensions() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x143283CD: vtkOpenGLExtensionManager::Update() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x14323BA3: vtkOpenGLExtensionManager::ExtensionSupported(char const*) (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x14371259: vtkOpenGLRenderWindow::OpenGLInitContext() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x1436FBCC: vtkOpenGLRenderWindow::OpenGLInit() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x1444238C: vtkXOpenGLRenderWindow::Start() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x144377BF: vtkXRenderWindowInteractor::Initialize() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082==    by 0x1B2175A2: vtkRenderWindowInteractor::Start() (in /usr/lib64/vtk/libvtkRenderingCore.so.1)
==3082==  If you believe this happened as a result of a stack
==3082==  overflow in your program's main thread (unlikely but
==3082==  possible), you can try to increase the size of the
==3082==  main thread stack using the --main-stacksize= flag.
==3082==  The main thread stack size used in this run was 8388608.

`

The following modifications to the CMakeList.txt file made it running. This is a solution but it does not explain why. If anyone could elaborate on why this is a solution please do.

I had to change the way to link the libraries. therefore I removed the line

target_link_libraries(uvlm debug ${GTE_DEBUG_CXX_LIBRARY} ${USEFULFCTS_DEBUG_CXX_LIBRARY} ${VTK_LIBRARIES} optimized ${GTE_RELEASE_CXX_LIBRARY} ${USEFULFCTS_RELEASE_CXX_LIBRARY} ${OpenMP_CXX_LIBRARIES} ${VTK_LIBRARIES})

and changed it into

if(CMAKE_BUILD_TYPE MATCHES "Debug")
    target_link_libraries(uvlm ${GTE_DEBUG_CXX_LIBRARY} ${USEFULFCTS_DEBUG_CXX_LIBRARY} ${VTK_LIBRARIES})
else()
    target_link_libraries(uvlm ${GTE_RELEASE_CXX_LIBRARY} ${USEFULFCTS_RELEASE_CXX_LIBRARY} ${VTK_LIBRARIES})
endif()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM