simple C++ OpenCV imshow example crashes with segmentation fault

Question

(sorry for bad English, I'm German)

Hello programmers,

In theory, this simple C++ OpenCV example should display a 100x100 black image, wait until a key a pressed, write "still running!\n" into the error-stream and exit with exit code 123.

When I run this program I see the black image until I hit a key. It seems like everythings works normal until I take a look at the terminal:

$ ./a.out
still running!
Segmentation fault (Core dumped) #shouldn't happen
$ echo $?
139       #should be 123
$

I would be thankful if anyone could answer one of the following questions to me: - Why does this program produces a segmentation fault? - How can I fix this? - Is there something wrong with the source code? And if not, which library/program is responsible for this error?

I can hear you screaming: "But hey, if it only crashes if exit/return is called, why do you care? It doesn't make a difference if the program exits normally or crashes". You are right, but I fear that the same bug that causes this program to crash while exiting might cause the program to crash while running / cause other wierd problems.

#include <opencv2/opencv.hpp>
#include <iostream>
int main(void) {
    {
        cv::Mat test(100, 100, CV_8UC1, cv::Scalar(0));
        //cv::namedWindow( "testName", cv::WINDOW_NORMAL ); //doesn't help
        //cv::namedWindow( "testName", cv::WINDOW_OPENGL ); //doesn't help
        cv::imshow( "testName", test );
        cv::waitKey(0);
        //cv::destroyAllWindows(); //doesn't help
    }
    std::cerr << "still running!\n";
    return 123;
}

Compilation: $ g++ pkg-config --cflags --libs opencv main.cpp or $ g++ -lopencv_core -lopencv_highgui main.cpp

Some additional informations about my PC:

OS: Linux (Fedora 25)

$ cat /proc/version

Linux version 4.8.14-300.fc25.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 6.2.1 20160916 (Red Hat 6.2.1-2) (GCC) ) #1 SMP Mon Dec 12 16:31:04 UTC 2016

OpenCV was installed with the following command: dnf install -y opencv*

Entering dnf remove opencv and hitting tab produces the following output:

opencv-3.1.0-8.fc25.x86_64             opencv-devel-3.1.0-8.fc25.x86_64
opencv-contrib-3.1.0-8.fc25.x86_64     opencv-devel-docs-3.1.0-8.fc25.noarch
opencv-core-3.1.0-8.fc25.x86_64        opencv-python-3.1.0-8.fc25.x86_64

UPDATE:

$ valgrind ./a.out
==24083== Memcheck, a memory error detector
==24083== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==24083== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==24083== Command: ./a.out
==24083== 
^C==24083== 
==24083== Process terminating with default action of signal 2 (SIGINT)
==24083==    at 0x401B4B5: open (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4005547: open_verify.constprop.7 (in /usr/lib64/ld-2.24.so)
==24083==    by 0x40090BF: _dl_map_object (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400DDFF: openaux (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4010873: _dl_catch_error (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400E44D: _dl_map_object_deps (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4003146: dl_main (in /usr/lib64/ld-2.24.so)
==24083==    by 0x401991E: _dl_sysdep_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4004F67: _dl_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4000CD7: ??? (in /usr/lib64/ld-2.24.so)
==24083== Jump to the invalid address stated on the next line
==24083==    at 0x5A6: ???
==24083==    by 0x4005547: open_verify.constprop.7 (in /usr/lib64/ld-2.24.so)
==24083==    by 0x40090BF: _dl_map_object (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400DDFF: openaux (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4010873: _dl_catch_error (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400E44D: _dl_map_object_deps (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4003146: dl_main (in /usr/lib64/ld-2.24.so)
==24083==    by 0x401991E: _dl_sysdep_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4004F67: _dl_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4000CD7: ??? (in /usr/lib64/ld-2.24.so)
==24083==  Address 0x5a6 is not stack'd, malloc'd or (recently) free'd
==24083== 
==24083== 
==24083== Process terminating with default action of signal 11 (SIGSEGV)
==24083==  Bad permissions for mapped region at address 0x5A6
==24083==    at 0x5A6: ???
==24083==    by 0x4005547: open_verify.constprop.7 (in /usr/lib64/ld-2.24.so)
==24083==    by 0x40090BF: _dl_map_object (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400DDFF: openaux (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4010873: _dl_catch_error (in /usr/lib64/ld-2.24.so)
==24083==    by 0x400E44D: _dl_map_object_deps (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4003146: dl_main (in /usr/lib64/ld-2.24.so)
==24083==    by 0x401991E: _dl_sysdep_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4004F67: _dl_start (in /usr/lib64/ld-2.24.so)
==24083==    by 0x4000CD7: ??? (in /usr/lib64/ld-2.24.so)
==24083== 
==24083== HEAP SUMMARY:
==24083==     in use at exit: 0 bytes in 0 blocks
==24083==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==24083== 
==24083== All heap blocks were freed -- no leaks are possible
==24083== 
==24083== For counts of detected and suppressed errors, rerun with: -v
==24083== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (Speicherabzug geschrieben)

UPDATE #2

$ g++ -g `pkg-config --cflags --libs opencv` main.cpp; gdb a.out
GNU gdb (GDB) Fedora 7.12-29.fc25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) run
Starting program: /home/volker/Dropbox/Roboter/eclipse/CVS/source/a.out 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.24-3.fc25.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7fffc86e1700 (LWP 5063)]
[New Thread 0x7fffbd3b9700 (LWP 5072)]
[New Thread 0x7fffb5974700 (LWP 5089)]
[New Thread 0x7fffb5173700 (LWP 5090)]
[New Thread 0x7fffb4972700 (LWP 5091)]
[New Thread 0x7fffa796c700 (LWP 5100)]
still running!

Thread 7 "QDBusConnection" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffa796c700 (LWP 5100)]
0x00007fffefa5601d in QObject::disconnect(QObject const*, char const*, QObject const*, char const*) () from /lib64/libQt5Core.so.5
Missing separate debuginfos, use: dnf debuginfo-install LONG LIST OF PACKAGES
(gdb) backtrace
#0  0x00007fffefa5601d in QObject::disconnect(QObject const*, char const*, QObject const*, char const*) () from /lib64/libQt5Core.so.5
#1  0x00007fffbec6bfb0 in QDBusConnectionPrivate::closeConnection() () from /lib64/libQt5DBus.so.5
#2  0x00007fffbec58852 in QDBusConnectionManager::run() () from /lib64/libQt5DBus.so.5
#3  0x00007fffef88b9da in QThreadPrivate::start(void*) () from /lib64/libQt5Core.so.5
#4  0x00007ffff077e6ca in start_thread () from /lib64/libpthread.so.0
#5  0x00007ffff0a9cf6f in clone () from /lib64/libc.so.6
(gdb) 

Show source
| linux   | c++   | opencv   | segmentation-fault   2016-12-20 12:12 0 Answers

Answers to simple C++ OpenCV imshow example crashes with segmentation fault ( 0 )

Leave a reply to - simple C++ OpenCV imshow example crashes with segmentation fault

◀ Go back