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

相关问题