qq_21019127 2015-12-07 02:02 采纳率: 100%
浏览 1316
已采纳

求大神寻找这段写视频代码的内存泄漏之处,菜鸟我实在找不出了

求大神围观~

 // OpenCVFindContours.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"  
#include "cxcore.h"   
#include "cv.h"   
#include "highgui.h"  

int _tmain(int argc, _TCHAR* argv[])
{
    CvCapture* pCapture = cvCreateCameraCapture(0);
    IplImage * pFrame;
    IplImage *psrc;
    IplImage *src = cvCreateImage(cvSize(700,700), IPL_DEPTH_8U,3);
    IplImage *gsrc = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U,1);

    IplImage *dsw ;
    IplImage *dst;
    CvMemStorage *storage;
    CvSeq *first_contour;

    while(1)
    {
        psrc=cvQueryFrame(pCapture ); 

        cvResize(psrc,src,1);
        cvCvtColor(src,gsrc,CV_BGR2GRAY);

        dsw = cvCreateImage(cvGetSize(src), 8, 1);  
        dst = cvCreateImage(cvGetSize(src), 8, 3);

        storage = cvCreateMemStorage(0);  
        first_contour = NULL;  

        //turn the src image to a binary image  
        //cvThreshold(src, dsw, 125, 255, CV_THRESH_BINARY_INV);  
        cvThreshold(gsrc, dsw, 100, 255, CV_THRESH_BINARY);  

        cvFindContours(dsw, storage, &first_contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);  
        cvZero(dst);  
        int cnt = 0;  
        for(; first_contour != 0; first_contour = first_contour->h_next)  
        {  
            cnt++;  
            CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255);  
            cvDrawContours(dst, first_contour, color, color, 0, 2, CV_FILLED, cvPoint(0, 0));  
            CvRect rect = cvBoundingRect(first_contour,0);
            cvRectangle(dst, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255, 0, 0), 1, 8, 0);
        }  

        printf("the num of contours : %d\n", cnt);  

        cvNamedWindow( "Source", 1 );  
        cvShowImage( "Source", src );  

        cvNamedWindow( "dsw", 1 );  
        cvShowImage( "dsw", dsw );  

        cvNamedWindow( "Components", 1 );  
        cvShowImage( "Components", dst );  

    cvReleaseMemStorage(&storage);

        char c=cvWaitKey(10);
        if(c==27)
            break;  
    }

    cvDestroyWindow("Source");
    cvDestroyWindow("dsw");
    cvDestroyWindow("Components");

    cvReleaseImage(&pFrame);
    cvReleaseImage(&gsrc);
    cvReleaseImage(&src);
    cvReleaseImage(&dsw);
    cvReleaseImage(&dst);

    //cvReleaseMemStorage(&storage);
    cvReleaseCapture(&pCapture);

    return 0;  
}

  • 写回答

1条回答 默认 最新

  • 云霏阳 2015-12-07 08:22
    关注

    1.建议将下面2行代码移到while循环里面最后的位置看看。
    cvReleaseImage(&dsw);
    cvReleaseImage(&dst);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决