2016-03-03 43 views
-2

我想在Fedora 23計算機上編譯下面的C代碼(gcc version 5.3.1 20151207(Red Hat 5.3.1-2)( GCC)),它不會給出任何錯誤。程序運行。下面的C代碼在Fedora上運行,但是在RaspberryPi 1上,給出了分段錯誤

但是,當我在Raspberry Pi 1 Model B +(gcc版本4.9.2(Raspbian 4.9.2-10))上編譯相同的代碼時,編譯器不會提供任何錯誤,但會在運行時崩潰。可以找出哪裏,問題?

還附加了調試信息。

/* Compile with 
* gcc -lm opencv_video_isolated.c -o ooopencv_video_isolated `pkg-config --cflags --libs opencv` 
*/ 

#include <stdio.h> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv/cv.h> 
#include <opencv/cxcore.h> 
#include <X11/Xlib.h> 

    int main() 
    { 
     CvCapture* capture; 

     capture = cvCreateCameraCapture(0); 

     IplImage* frame; 

     /* Capture a single frame from the video stream */ 
     frame = cvQueryFrame(capture); 


     double ab = frame->depth; 
     double ac = frame->width; 
     double ad = frame->height; 



     return 0; 
    } 

此外,我已經在Raspberry Pi上粘貼了GDB輸出。

(gdb) file opencv_video_isolated 
Reading symbols from opencv_video_isolated...done. 
(gdb) run 
Starting program: /home/.../src/opencv_video_isolated 
[Thread debugging using libthread_db enabled] 
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1". 
[New Thread 0xb30ec270 (LWP 1523)] 

Program received signal SIGSEGV, Segmentation fault. 
0x00012718 in main() at opencv_video_isolated.c:22 
22  double ab = frame->depth; 
(gdb) backtrace 
#0 0x00012718 in main() at opencv_video_isolated.c:22 
(gdb) 
+2

你可能想要添加一些基本的防禦性編程技巧,比如檢查返回值。 –

回答

2

Program received signal SIGSEGV, Segmentation fault.

你要print frame。最有可能的是NULL。然後,您需要弄清楚您違反了cvQueryFramecvCreateCameraCapture的前提條件。

+1

是的。問題是'cvQueryFrame'返回一個** NULL **。有沒有一種最好的防守測試方法比以下更實用? 'frame = cvQueryFrame(capture); if(!frame) printf(「cvQueryFrame Failed \ n」);' – inckka

相關問題