Solved - Segmentation fault (core dumped) - When running simple OpenCV compiled code

Hello, I am reading the book: "OpenCV " - 2nd edition - By Adrian Kaehler & Gary Bradski.

Right at the very first code example in the book, “First Program - Display a Picture”, I get an error: Segmentation fault (core dumped).

Here is the code example from the book:
Example 2-2: A simple OpenCV program that loads an image from disk and displays it on the screen

#include "opencv2/highgui/highgui.hpp" 

using namespace cv; 

int main( int argc, char** argv ) 
  Mat img = imread( argv[1], -1 ); 

    if( img.empty() ) return -1; 

    namedWindow( "Example2", WINDOW_AUTOSIZE ); 

    imshow( "Example2", img ); 

    waitKey( 0 ); 

    destroyWindow( "Example2" ); 

I used clang to compile the code using:

clang++ -g opencv_1.cpp -o opencv_1 $(pkg-config --cflags --libs opencv4) -I /usr/local/include/opencv4

I did not get any errors during and after compilation.

When I run the compiled code using:

I get the following error:
Segmentation fault (core dumped)

Now I have no clue whats causing this error.

Here are the details about my PC:

  • OS: FreeBSD 13.1-RELEASE-p1 amd64
  • Shell: sh
  • Resolution: 3840x2160
  • DE: Plasma 5.24.6
  • WM: KWin
  • Theme: [Plasma], Breeze [GTK2/3]
  • Icons: [Plasma], breeze-dark [GTK2/3]
  • Terminal: konsole
  • CPU: AMD FX-8350 (8) @ 3.991GHz
  • GPU: Ellesmere [Radeon RX 580]
  • Memory: 5755MiB / 32684MiB

I have compiled OpenCV 4.5.5_9 from source using:
cd /usr/ports/graphics/opencv/ && make reinstall clean

I have also tried running the OpenCV compiled C++ code using the pkg installation of OpenCV 4.5.5_9 rather than using the compilation of FreeBSD ports source:
pkg install graphics/opencv

Doesn’t matter if is use either the pkg or ports version of OpenCV, I get the same error.

Here are some details about the OpenCV which is currently installed.
(Note: this is the compilation version from FreeBSD Ports on graphics/OpenCV):

pkg info opencv

Name : opencv
Version : 4.5.5_9
Installed on : Fri Aug 26 22:54:00 2022 EDT
Origin : graphics/opencv
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : graphics
Licenses : BSD3CLAUSE
Maintainer :
Comment : Open Source Computer Vision library
Options :
ATLAS : off
DC1394 : on
EIGEN : on
FFMPEG : off
GDAL : on
GDCM : on
GPHOTO2 : on
GTK3 : off
JASPER : off
JAVA : off
JPEG : on
LTO : off
NOBLAS : off
PNG : on
TBB : off
TIFF : on
V4L : on
VTK : off
WEBP : on
XINE : off
Shared Libs required:
Shared Libs provided:
Annotations :
FreeBSD_version: 1301000
cpe : cpe:2.3:a:opencv:opencv:4.5.5:::::freebsd13:x64:9
Flat size : 96.8MiB

Anyone have any clue whats causing this error for such basic code?

Thanks for any advice.

throw a try-catch around everything, see if you get a cv::Exception, and if you do, print its details.

if nobody’s holding a gun to your head to use C++, ditch it and start with python. no building required, official binary package by OpenCV, and python doesn’t just keel over when an exception happens.

1 Like

Thank You for your reply. I will try to implement “try-ctach”.

You executed the program without an image path as argument. Argv[1] does not exist and the program will of course crash…


Yes, I realized that.

I have also tried doing:

./opencv_1 lena.jpg

and now get this error:

Abort trap (core dumped)

Here is the gdb back trace debugging:

GNU gdb (GDB) 12.1 [GDB v12.1 for FreeBSD]
Reading symbols from /home/user/OpenCV_1/opencv_1...
[New LWP 128661]

warning: Unexpected size of section `.reg-xstate/128661' in core file.
Core was generated by `./opencv_1 lena.jpg'.
Program terminated with signal SIGABRT, Aborted.
Sent by thr_kill() from pid 3967 and user 1001.

warning: Unexpected size of section `.reg-xstate/128661' in core file.
#0  0x0000000804cb433a in thr_kill () from /lib/

(gdb) bt

#0  0x0000000804cb433a in thr_kill () at /lib/
#1  0x0000000804c2cc74 in raise () at /lib/
#2  0x0000000804cde109 in abort () at /lib/
#3  0x0000000800febe69 in  () at /lib/
#4  0x00000008041e98f5 in cv::error(cv::Exception const&) () at /usr/local/lib/
#5  0x00000008041e91a2 in cv::error(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*, char const*, int) ()
    at /usr/local/lib/
#6  0x0000000800db7417 in cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) () at /usr/local/lib/
#7  0x00000000002027a9 in main(int, char**) (argc=2, argv=0x7fffffffe478) at opencv_1.cpp:28

Trace shows exception was raised in cv::namedWindow()

I think OpenCV have some trouble to display the image, like literally. Because it could be that OpenCV does not know how to access the window/desktop server to display the image on the KDE desktop environment I am using.

Does OpenCV works out of the box for FreeBSD 13.1 using KDE5 plasma desktop environment, does it need like GTK or QT to work?

haven’t you caught the cv::Exception? why mess around with the debugger before you even know the exception’s contents? it’s C++, it can do that.

in general, OpenCV people don’t concern themselves much with operating systems or desktop environments. those things are supposed to work and to get out of the way.

on unixy platforms that aren’t apple, it needs GTK or Qt, yes. what did you expect? it talking to an X server directly?

1 Like

I’m not too sure how to catch the exception using the try and catch method, never played around much of it. (yes, I am beginner c++) I’ll figure it out soon but I do know how to play around with the debugger.

I think the gtk is disabled on opencv. I do know I have gtk3-3.24.34 installed in the OS.

I have come up with this code:

#include <iostream>
#include "opencv2/highgui/highgui.hpp" 

using namespace cv; 

int main( int argc, char** argv ) 
    int e;

        Mat img = imread( argv[1]); 

        if( img.empty() ) {return -1;}

        namedWindow( "Example1", WINDOW_AUTOSIZE );

        imshow( "Example1", img ); 

        waitKey( 0 ); 

        destroyWindow( "Example1" );

        throw e;

    catch (int e)
        std::cout << "An exception occurred. Exception Nr. " << e << '\n';
    return 0;

I do not get any exceptions, same error as to the 1st post.


I was able to fix the problem. I needed to recompile OpenCV with GTK3 enabled since for some reason it is disabled for the FreeBSD’s distribution of OpenCV. I would like to know if anyone was able to get OpenCV 4.6.0 compiled and working on FreeBSD 13.1 since 4.6.0 is not available on the FreeBSD distribution yet.

1 Like

exceptions can be of different types. you aren’t looking to catch an exception of type int, but of type cv::Exception. that is why I gave you this link that shows you exactly what you need:

use whatever version you like. they all can do the basic stuff.