尝试使用 open CV 和 SDL 追踪内存泄漏

发布于 2024-12-07 09:38:10 字数 3570 浏览 1 评论 0原文

下面的程序主循环是我在查找内存泄漏时遇到问题的地方。我运行 Top,每次循环拍照和打印时,我都会丢失内存,即使退出也永远不会恢复。我运行了 valgrind,一些结果在底部。该程序运行良好,直到内存耗尽。我似乎有杯子和 opencv 泄漏,泄漏不是几百个字节,它很重要 - 我感谢任何帮助

Ubuntu 11.04 opencv 2.3.1

void DrawImage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha) {
  if ((!srcimg) || (alpha == 0)) return; //If theres no image, or its 100% transparent.
  SDL_Rect src, dst;
  src.x = sx;  src.y = sy;  src.w = sw;  src.h = sh;
  dst.x = dx;  dst.y = dy;  dst.w = src.w;  dst.h = src.h;
  SDL_BlitSurface(srcimg, &src, dstimg, &dst);
}


// ************************ Start of Main Loop ************************

while(Leave == 0)   
{

if(Start != 0)
{
    atr = IMG_Load("pic1.jpg");
    DrawImage(atr, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
    SDL_Flip(screen);

    CvCapture *camera = cvCreateCameraCapture(-1); 
    IplImage *frame2; 

    SDL_Surface* surface = NULL;

    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, IMG_HEIGHT);
    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, IMG_WIDTH);

    surface = IMG_Load("background.jpg");

    Seconds = 15;
    while(Seconds !=0)
    {
       frame2 = cvQueryFrame(camera);
       if(!frame2)continue; //Couldn't get an image, try again next time.

       SDL_Surface* surface2 = NULL;

       surface2 = SDL_CreateRGBSurfaceFrom((void*)frame2->imageData,
              frame2->width,
              frame2->height,
              frame2->depth*frame2->nChannels,
              frame2->widthStep,
              0xff0000, 0x00ff00, 0x0000ff, 0);

      SDL_BlitSurface(surface2, NULL, surface, &offsetpic);

      SDL_FreeSurface(surface2);

      DrawImage(surface, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
      SDL_Flip(screen);

    }

  if(!cvSaveImage("lastprint.jpg",frame2,0)) printf("Could not save: lastprint.jpg");

  cvReleaseImage(&frame2);
  cvReleaseCapture(&camera); //Release the camera capture structure.
  SDL_FreeSurface(surface);

  cupsPrintFile(dest->name, "lastprint.jpg", "JOB1", dest->num_options, dest->options);
  if(Start !=0) Start--;                // Dec Start

//      atr = IMG_Load("pic1.jpg");
  DrawImage(atr, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
  SDL_Flip(screen);

  }
}

这里直接下面的许多 vvvvv

32,780 字节 在 在 0x4026864处的 182 条丢失记录 180 中仍然可以访问 1 个块

:malloc (vg_replace_malloc.c:236)

由 0x4365BA7: ??? (在/usr/lib/libcups.so.2中)

由0x436731E:ippReadIO(在/usr/lib/libcups.so.2中)

由0x436785C:ippReadIO(在/usr/lib/libcups.so.2中)

由0x436785C: ippReadIO(在/usr/lib/libcups.so.2中

) 0x4367E85:ippRead(在/usr/lib/libcups.so.2中)

通过0x437A173:cupsGetResponse(在/usr/lib/libcups.so.2中)

通过0x437A501:cupsDoIORequest(在/usr/lib/libcups.so.2中)

通过 0x437A6FA:cupsDoRequest(在/usr/lib/libcups.so.2)

由 0x4358386: ??? (在/usr/lib/libcups.so.2中)

通过0x4359D52:cupsGetDests2(在/usr/lib/libcups.so.2中)

通过0x435A1B4:cupsGetDests(在/usr/lib/libcups.so.2中)

1 个块中的 1,440,020 字节可能在丢失记录 181 of 182 中丢失

在 0x4026864: malloc (vg_replace_malloc.c:236)

by 0x415D0EB: cv::fastMalloc(unsigned int) (in /usr/local/lib/libopencv_core.so.2.3.1)

by 0x4A5DE36: (main 下面) (libc-start .c:226) ---DrawImage是main下面唯一的东西

4 个块中的 67,108,864 字节可能在

0x4026864 处的 182 条丢失记录 182 中丢失:malloc (vg_replace_malloc.c:236)

by 0x473C057: _capture_V4L2(CvCaptureCAM_V4L*, char*)(在/usr/local/lib/libopencv_highgui.so.2.3.1)

The program main loop below is where I am having troubles finding a memory leak. I run Top and every time I loop through taking a picture and printing it, I lose memory that is never recovered even upon exit. I have run valgrind and some results are at the bottom. The progam runs fine until it runs out of memory. I seem to have leaks with cups and opencv and the leak is not a couple hundred bytes it is significant - I appreciate any help

Ubuntu 11.04 opencv 2.3.1

void DrawImage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha) {
  if ((!srcimg) || (alpha == 0)) return; //If theres no image, or its 100% transparent.
  SDL_Rect src, dst;
  src.x = sx;  src.y = sy;  src.w = sw;  src.h = sh;
  dst.x = dx;  dst.y = dy;  dst.w = src.w;  dst.h = src.h;
  SDL_BlitSurface(srcimg, &src, dstimg, &dst);
}


// ************************ Start of Main Loop ************************

while(Leave == 0)   
{

if(Start != 0)
{
    atr = IMG_Load("pic1.jpg");
    DrawImage(atr, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
    SDL_Flip(screen);

    CvCapture *camera = cvCreateCameraCapture(-1); 
    IplImage *frame2; 

    SDL_Surface* surface = NULL;

    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, IMG_HEIGHT);
    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, IMG_WIDTH);

    surface = IMG_Load("background.jpg");

    Seconds = 15;
    while(Seconds !=0)
    {
       frame2 = cvQueryFrame(camera);
       if(!frame2)continue; //Couldn't get an image, try again next time.

       SDL_Surface* surface2 = NULL;

       surface2 = SDL_CreateRGBSurfaceFrom((void*)frame2->imageData,
              frame2->width,
              frame2->height,
              frame2->depth*frame2->nChannels,
              frame2->widthStep,
              0xff0000, 0x00ff00, 0x0000ff, 0);

      SDL_BlitSurface(surface2, NULL, surface, &offsetpic);

      SDL_FreeSurface(surface2);

      DrawImage(surface, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
      SDL_Flip(screen);

    }

  if(!cvSaveImage("lastprint.jpg",frame2,0)) printf("Could not save: lastprint.jpg");

  cvReleaseImage(&frame2);
  cvReleaseCapture(&camera); //Release the camera capture structure.
  SDL_FreeSurface(surface);

  cupsPrintFile(dest->name, "lastprint.jpg", "JOB1", dest->num_options, dest->options);
  if(Start !=0) Start--;                // Dec Start

//      atr = IMG_Load("pic1.jpg");
  DrawImage(atr, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255);
  SDL_Flip(screen);

  }
}

Many of the one directly below here vvvvv

32,780 bytes in 1 blocks are still reachable in loss record 180 of 182

at 0x4026864: malloc (vg_replace_malloc.c:236)

by 0x4365BA7: ??? (in /usr/lib/libcups.so.2)

by 0x436731E: ippReadIO (in /usr/lib/libcups.so.2)

by 0x436785C: ippReadIO (in /usr/lib/libcups.so.2)

by 0x436785C: ippReadIO (in /usr/lib/libcups.so.2)

by 0x4367E85: ippRead (in /usr/lib/libcups.so.2)

by 0x437A173: cupsGetResponse (in /usr/lib/libcups.so.2)

by 0x437A501: cupsDoIORequest (in /usr/lib/libcups.so.2)

by 0x437A6FA: cupsDoRequest (in /usr/lib/libcups.so.2)

by 0x4358386: ??? (in /usr/lib/libcups.so.2)

by 0x4359D52: cupsGetDests2 (in /usr/lib/libcups.so.2)

by 0x435A1B4: cupsGetDests (in /usr/lib/libcups.so.2)

1,440,020 bytes in 1 blocks are possibly lost in loss record 181 of 182

at 0x4026864: malloc (vg_replace_malloc.c:236)

by 0x415D0EB: cv::fastMalloc(unsigned int) (in /usr/local/lib/libopencv_core.so.2.3.1)

by 0x4A5DE36: (below main) (libc-start.c:226) ---DrawImage is the only thing below main

67,108,864 bytes in 4 blocks are possibly lost in loss record 182 of 182

at 0x4026864: malloc (vg_replace_malloc.c:236)

by 0x473C057: _capture_V4L2(CvCaptureCAM_V4L*, char*) (in /usr/local/lib/libopencv_highgui.so.2.3.1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-12-14 09:38:10

好吧,至少您永远不会在 atr 上调用 SDL_FreeSurface()

Well, at a minimum you never call SDL_FreeSurface() on atr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文