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:
./opencv_1

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

opencv-4.5.5_9
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 : desktop@FreeBSD.org
WWW : https://www.opencv.org/
Comment : Open Source Computer Vision library
Options :
ATLAS : off
DC1394 : on
EIGEN : on
FFMPEG : off
GDAL : on
GDCM : on
GPHOTO2 : on
GSTREAMER : on
GTK3 : off
HARDENING : off
JASPER : off
JAVA : off
JPEG : on
LTO : off
NOBLAS : off
OPENBLAS : on
OPENCL : on
OPENEXR : on
OPENJPEG : on
PATENTED : off
PNG : on
PROTOBUF : on
PYTHON : on
TBB : off
TESSERACT : off
TIFF : on
V4L : on
VTK : off
VULKAN : on
WEBP : on
XINE : off
Shared Libs required:
libwebp.so.7
libtiff.so.5
libpng16.so.16
libopenjp2.so.7
libopenblas.so.0
libjpeg.so.8
libhdf5.so.200
libharfbuzz.so.0
libgstvideo-1.0.so.0
libgstriff-1.0.so.0
libgstreamer-1.0.so.0
libgstpbutils-1.0.so.0
libgstbase-1.0.so.0
libgstaudio-1.0.so.0
libgstapp-1.0.so.0
libgphoto2_port.so.12
libgphoto2.so.6
libgobject-2.0.so.0
libglog.so.1
libglib-2.0.so.0
libgdcmMSFF.so.3.0
libgdcmDSED.so.3.0
libgdal.so.3
libfreetype.so.6
libdc1394.so.25
libOpenEXR-3_1.so.30
Shared Libs provided:
libopencv_xphoto.so.405
libopencv_xobjdetect.so.405
libopencv_ximgproc.so.405
libopencv_xfeatures2d.so.405
libopencv_wechat_qrcode.so.405
libopencv_videostab.so.405
libopencv_videoio.so.405
libopencv_video.so.405
libopencv_tracking.so.405
libopencv_text.so.405
libopencv_surface_matching.so.405
libopencv_superres.so.405
libopencv_structured_light.so.405
libopencv_stitching.so.405
libopencv_stereo.so.405
libopencv_shape.so.405
libopencv_sfm.so.405
libopencv_saliency.so.405
libopencv_rgbd.so.405
libopencv_reg.so.405
libopencv_rapid.so.405
libopencv_quality.so.405
libopencv_plot.so.405
libopencv_photo.so.405
libopencv_phase_unwrapping.so.405
libopencv_optflow.so.405
libopencv_objdetect.so.405
libopencv_ml.so.405
libopencv_mcc.so.405
libopencv_line_descriptor.so.405
libopencv_intensity_transform.so.405
libopencv_imgproc.so.405
libopencv_imgcodecs.so.405
libopencv_img_hash.so.405
libopencv_highgui.so.405
libopencv_hfs.so.405
libopencv_hdf.so.405
libopencv_gapi.so.405
libopencv_fuzzy.so.405
libopencv_freetype.so.405
libopencv_flann.so.405
libopencv_features2d.so.405
libopencv_face.so.405
libopencv_dpm.so.405
libopencv_dnn_superres.so.405
libopencv_dnn_objdetect.so.405
libopencv_dnn.so.405
libopencv_datasets.so.405
libopencv_core.so.405
libopencv_ccalib.so.405
libopencv_calib3d.so.405
libopencv_bioinspired.so.405
libopencv_bgsegm.so.405
libopencv_barcode.so.405
libopencv_aruco.so.405
libopencv_alphamat.so.405
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…

2 Likes

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/libc.so.7

(gdb) bt

#0  0x0000000804cb433a in thr_kill () at /lib/libc.so.7
#1  0x0000000804c2cc74 in raise () at /lib/libc.so.7
#2  0x0000000804cde109 in abort () at /lib/libc.so.7
#3  0x0000000800febe69 in  () at /lib/libcxxrt.so.1
#4  0x00000008041e98f5 in cv::error(cv::Exception const&) () at /usr/local/lib/libopencv_core.so.405
#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/libopencv_core.so.405
#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/libopencv_highgui.so.405
#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;

    try
    {
        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.

Edit:

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.