• Uncategorized

About c++ : Program-terminated-with-signal-sigsegv-segmentation-fault-0-0x000000000040199e-in

Question Detail

I am a beginner for opencv. Yesterday I wrote a simple code in c++, which is really puzzled me. Here is my code.

#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void cvtColor_cv()
{
    Mat image;
    Mat image2;
    image=imread("/home/shz/alltest/c++/640.jpg",1);
    namedWindow("Display Image",WINDOW_AUTOSIZE);
    namedWindow("Display Image2",WINDOW_AUTOSIZE);
    imshow("Display Image",image);
    cvtColor(image,image2,COLOR_BGR2Luv);
    imshow("Display Image2",image2);
    waitKey(0);
}
void cvtColor_cal()
{
    Mat image;
    Mat image2;
    double a[3][3];
    image=imread("/home/shz/alltest/c++/640.jpg",1);
    int height=image.rows;
    int width=image.cols;
    cout << height <<endl;
    cout << width <<endl;
    //Vec3b bgr=image.at<Vec3b>(0,0);
    cout << image.at<Vec3b>(0,0)[0] << endl;
    
    //namedWindow("Display Image",WINDOW_AUTOSIZE);
    //namedWindow("Display Image2",WINDOW_AUTOSIZE);
    //imshow("Display Image",image);
    for (int i=1;i<height;i++)
    {
        for (int j=1;j<width;j++)
        {   
            
            //cout << (int) data[j] <<endl;
            //Vec3b bgr=image.at<Vec3b>(i,j);
            cout << image.at<Vec3b>(i,j)[0] <<endl;
                
        }
    }       
    //image=1/0.17697*a*image;
    //imshow("Display Image2",image);
    
    waitKey(0);
}
int main()
{
    cvtColor_cal();
    return 0;
}

As we can see ,the output is:

746
1080
�
Segmentation fault (core dumped)

After I use GDB to debug this program and it prompts me:

program terminated with signal sigsegv, segmentation fault. #0 0x000000000040199e in ?? ()

Obviously, image.rows and image.cols worked well. im.at<Vec3b>(0,0)[0] also worked but the output is a little strange.The for loops that I have tested can work well done(I used it to output a lot number “1”).

But when I write im.at<Vec3b>(i,j)[0] into for loops. It has reported the above error.

This is really a hard question for me. The version of OpencV I use is 4.5.5. The version of gcc I use is 5.4.0.

The GDB result is here:

(gdb) r
Starting program: /home/shz/alltest/test_2022.4.9/Display 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
746
1080
�

Breakpoint 1, cvtColor_cal () at Display.cpp:49
49              Vec3b bgr=image.at<Vec3b>(i,j);
(gdb) n

Breakpoint 2, cv::Matx<unsigned char, 3, 1>::Matx (this=0x7fffffffe4b0, 
    values=0x3fd51eb851eb91ca <error: Cannot access memory at address 0x3fd51eb851eb91ca>) at /usr/local/include/opencv2/core/matx.hpp:673
673     for( int i = 0; i < channels; i++ ) val[i] = values[i];
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401a58 in cv::Matx<unsigned char, 3, 1>::Matx (
    this=0x7fffffffe4b0, 
    values=0x3fd51eb851eb91ca <error: Cannot access memory at address 0x3fd51eb851eb91ca>) at /usr/local/include/opencv2/core/matx.hpp:673
673     for( int i = 0; i < channels; i++ ) val[i] = values[i];
(gdb) n

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
47              
48              //cout << (int) data[j] <<endl;
49              Vec3b bgr=image.at<Vec3b>(i,j);
50              cout << image.at<Vec3b>(i,j)[0] <<endl;
51                  
52          }
53      }       
54      //image=1/0.17697*a*image;
55      //imshow("Display Image2",image);
56  

Question Answer

  1. You’re familiar with using your debugger (gdb), how to set breakpoints, and how to single-step through the code: that’s good.

  2. You’re able to see exactly where the segmentation violation is occurring – that’s good, too:

    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000401a58 in cv::Matx<unsigned char, 3, 1>::Matx (
        this=0x7fffffffe4b0, 
       values=0x3fd51eb851eb91ca <error: Cannot access memory at address 0x3fd51eb851eb91ca>) at /usr/local/include/opencv2/core/matx.hpp:673
    
  3. Line 673 in matx.hpp is inside OpenCV. We need to know the line in YOUR code that’s CAUSING OpenCV to crash. I suspect it might be here:

    for (int i=1;i<height;i++) {
      for (int j=1;j<width;j++) {    
        cout << image.at<Vec3b>(i,j)[0] << endl; // <-- I suspect "i" and/or "j" might be incorrect
    
  4. SUGGESTION: instrument your code like this:

    std::cout << std::unitbuf; // enable automatic flushing
    for (int y=1;y<height;y++) {
      for (int x=1;x<width;x++) {    
        cout << "x=" << x << ", y=" << y << ": " << image.at<Vec3b>(x,y)[0] << endl;
    
    • I changed the variables “i” and “j” to “y” and “x”, and ensured they corresponded to the “vertical” and “horizontal” dimensions, respectively.
    • I print out each iteration for each pixel (the last x/y printed out before crashing is likely the culprit).
    • I added a unitbuf manipulator to ensure the line that crashes actually prints out.

I hope this will help you troubleshoot WHERE … and consequently WHY your app is crashing.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.