Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV imshow segmentation fault

I installed OpenCV 4.0 on the latest version of Raspbian today following this guide: https://docs.opencv.org/4.0.0/d7/d9f/tutorial_linux_install.html

I followed each required step exactly, skipped all optional ones, and ran cmake with no parameters. I also installed the stripped version because I was running into issues installing the full one.

Afterwards I ran sudo apt-get install libopencv-dev to resolve some linker issues.

I'm now trying to run a simple program to display an image, but it segfaults upon reaching imshow.

Here it is in its entirety:

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

using namespace cv;
using namespace std;

int main() {
    cout << "hello" << endl;

    Mat src = imread("test_img_1.jpg", IMREAD_COLOR);
    if (src.empty()) {
        cout << "read error" << endl;
    }

    namedWindow("Source", WINDOW_AUTOSIZE);
    imshow("Source", src);

    waitKey(0);
    return 0;
}

I'm compiling with g++ -I/usr/local/include/opencv4 main.cpp `pkg-config --cflags --libs opencv`

Running with gdb gives this output:

Thread 1 "a.out" received signal SIGSEGV, Segmentation fault.
0x76b24cdc in cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) ()
   from /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4

And running with valgrind gives this:

==5560== Memcheck, a memory error detector
==5560== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5560== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5560== Command: ./a.out
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x486603C: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6c48b4 is 12 bytes inside a block of size 18 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4866054: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6c5344 is 20 bytes inside a block of size 26 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865CA4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6cdc24 is 28 bytes inside a block of size 34 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865FC4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb6e1d64 is 20 bytes inside a block of size 26 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560== 
hello
==5560== Invalid read of size 8
==5560==    at 0x4865B38: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb800e34 is 12 bytes inside a block of size 17 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865D34: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb83cfa4 is 12 bytes inside a block of size 18 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x48660C8: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb841c24 is 12 bytes inside a block of size 19 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865BC4: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb84796c is 12 bytes inside a block of size 17 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865C78: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb848850 is 8 bytes inside a block of size 14 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865FAC: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb84b8cc is 12 bytes inside a block of size 19 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865E8C: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb871790 is 8 bytes inside a block of size 15 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 8
==5560==    at 0x4865BDC: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem.so)
==5560==  Address 0xb8f7ec4 is 28 bytes inside a block of size 34 alloc'd
==5560==    at 0x4847568: malloc (vg_replace_malloc.c:299)
==5560==    by 0x6508C2F: g_malloc (in /lib/arm-linux-gnueabihf/libglib-2.0.so.0.5000.3)
==5560== 
==5560== Invalid read of size 4
==5560==    at 0x4CFDCDC: cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) (in /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4.9)
==5560==  Address 0x1010018 is not stack'd, malloc'd or (recently) free'd
==5560== 
==5560== 
==5560== Process terminating with default action of signal 11 (SIGSEGV)
==5560==  Access not within mapped region at address 0x1010018
==5560==    at 0x4CFDCDC: cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) (in /usr/lib/arm-linux-gnueabihf/libopencv_highgui.so.2.4.9)
==5560==  If you believe this happened as a result of a stack
==5560==  overflow in your program's main thread (unlikely but
==5560==  possible), you can try to increase the size of the
==5560==  main thread stack using the --main-stacksize= flag.
==5560==  The main thread stack size used in this run was 8388608.

I noticed the valgrind output shows there are invalid reads before the first line of main executes, so perhaps this is the source of the problem? I'm not sure what the error means, though.

I have searched for similar issues on StackOverflow already and have not been able to find much. Please let me know if there's anything I can do to improve this question.

Edit: Don't think I made this clear--the program is never entering the "read error" block. I have since changed the code to return if it does enter that block, but it is still segfaulting.

Here is the installation information:

General configuration for OpenCV 4.0.0 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2018-12-30T23:20:59Z
    Host:                        Linux 4.14.79-v7+ armv7l
    CMake:                       3.7.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  CPU/HW features:
    Baseline:
      requested:                 DETECT
      disabled:                  VFPV3 NEON

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/c++  (ver 6.3.0)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -mfp16-format=ieee -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      
    Linker flags (Debug):        
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          dl m pthread rt
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python2 python3 python_bindings_generator stitching ts video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java js
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  GUI: 
    GTK+:                        YES (ver 2.24.31)
      GThread :                  YES (ver 2.50.3)
      GtkGlExt:                  NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.8)
    JPEG:                        libjpeg-turbo (ver 1.5.3-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.28)
    TIFF:                        build (ver 42 - 4.0.9)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (ver 57.64.101)
      avformat:                  YES (ver 57.56.101)
      avutil:                    YES (ver 55.34.101)
      swscale:                   YES (ver 4.2.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    linux/videodev2.h

  Parallel framework:            pthreads

  Trace:                         YES (built-in)

  Other third-party libraries:
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                /home/pi/opencv-4.0.0/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.13)
    Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.13)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)
    packages path:               lib/python2.7/dist-packages

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.5.3)
    Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)
    numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.12.1)
    packages path:               lib/python3.5/dist-packages

  Python (for build):            /usr/bin/python2.7

  Java:                          
    ant:                         /usr/bin/ant (ver 1.9.9)
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr/local
-----------------------------------------------------------------
like image 435
Phoebe Avatar asked Nov 06 '22 22:11

Phoebe


1 Answers

Just check the path of your image, it s a load problem so copy the image in the same directory as your cpp code or just give the "imread" an absolute path

like image 105
Spinkoo Avatar answered Nov 09 '22 22:11

Spinkoo