应用程序在读取35-40个摄像头帧后崩溃

Application crashes after reading 35-40 Webcam frames

本文关键字:崩溃 摄像头 35-40个 读取 应用程序      更新时间:2023-10-16

我已经成功地检测了从网络摄像头捕获的人脸,但是在捕获了大约35-40帧之后,应用程序崩溃了,我只发布了我代码的相关部分,_Image是我实现的一个类。

_Image *Obj;
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) 
{
   fprintf( stderr, "nn---ERROR: capture is NULL---n" );
}
IplImage * frame = cvQueryFrame( capture );
 // Show the image captured from the camera in the window and repeat
while (1) 
{
    frame = cvQueryFrame( capture );
    if ( !frame ) {
          fprintf( stderr, "ERROR: Webcam frame is null...Unexpected Error - Exiting" );    
          getchar();
          exit(0);
    }
    Obj = new _Image(frame);
    if(Obj==0)
    {
        fprintf( stderr, "nERROR: Out of Memory!!n" );
        cvReleaseImage(&frame );
        cvDestroyAllWindows();
        // Release the capture device 
        cvReleaseCapture( &capture );
                        exit(0);
    }
    flag = Obj->detect_face_in_image();
    if(flag!=0)
    {
        Obj->add_frame_name();
        Obj->webcam_reader( *Obj); 
    }
         // Do not release the frame!
    delete Obj;
    cvNamedWindow( "WEBCAM", CV_WINDOW_AUTOSIZE );
    cvShowImage ( "WEBCAM", frame);
    if ( (cvWaitKey(10)) == 27 )
    {
                  cvReleaseImage(&frame );
        cvDestroyAllWindows();
        // Release the capture device 
        cvReleaseCapture( &capture );
                        break;
    }
}   

然而,我没有得到这种崩溃,如果我改变我的代码一点播放AVI文件,这只发生在网络摄像头。我得到Windows XP send or dont send error report,崩溃前没有特定的错误信息

只要代码不完整,就不可能给出原因。

你正在传递*Obj到Obj->webcam_reader() -为什么,如果Obj已经作为this指针传递了。

根据webcam_reader是如何定义的,你可以在此时复制Obj(如果你不通过引用传递)。如果不能安全地复制该对象,您可能会遇到问题。如果您没有实现复制构造函数,而是在该类中使用手动分配的动态内存,则可能会出现这种情况。在这种情况下,只有一个指针被复制和释放两次。

如上所述,人们只能猜测原因。

change

cvWaitKey(10);

cvWaitKey(25);

或者

cvWaitKey(35);