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

**URL:** <https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979>\
**Category:** C++\
**Created:** [August 27, 2022, 6:07pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979 "2022-08-27T18:07:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unix\_Man](https://avatars.discourse-cdn.com/v4/letter/u/3ab097/32.png) [@Unix\_Man](https://forum.opencv.org/u/Unix_Man)\
**Post date:** [August 27, 2022, 6:07pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/1 "2022-08-27T18:07:10Z")

</div>

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_

```cpp
#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](mailto:desktop@FreeBSD.org)  
> WWW : [https://www.opencv.org/](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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 27, 2022, 6:15pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/2 "2022-08-27T18:15:33Z")

</div>

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

> <https://stackoverflow.com/questions/19488635/getting-opencv-error-in-c>

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.

---

<div class="post-metadata">

**Author:** ![Unix\_Man](https://avatars.discourse-cdn.com/v4/letter/u/3ab097/32.png) [@Unix\_Man](https://forum.opencv.org/u/Unix_Man)\
**Post date:** [August 27, 2022, 6:20pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/3 "2022-08-27T18:20:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [August 27, 2022, 7:44pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/4 "2022-08-27T19:44:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Unix\_Man](https://avatars.discourse-cdn.com/v4/letter/u/3ab097/32.png) [@Unix\_Man](https://forum.opencv.org/u/Unix_Man)\
**Post date:** [August 27, 2022, 9:57pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/5 "2022-08-27T21:57:39Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 27, 2022, 10:43pm UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/6 "2022-08-27T22:43:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Unix\_Man](https://avatars.discourse-cdn.com/v4/letter/u/3ab097/32.png) [@Unix\_Man](https://forum.opencv.org/u/Unix_Man)\
**Post date:** [August 28, 2022, 12:12am UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/7 "2022-08-28T00:12:33Z")

</div>

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:

```auto
#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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 28, 2022, 9:32am UTC](https://forum.opencv.org/t/solved-segmentation-fault-core-dumped-when-running-simple-opencv-compiled-code/9979/8 "2022-08-28T09:32:30Z")

</div>

> [@Unix\_Man](#):
>
> ` catch (int e)`

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:

> [@crackwitz](#):
>
> [Getting opencv error in c++ - Stack Overflow](https://stackoverflow.com/questions/19488635/getting-opencv-error-in-c)

> [@Unix\_Man](#):
>
> since 4.6.0 is not available on the FreeBSD distribution yet

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