【问题标题】:trying to track down a Memory Leak using open CV and SDL尝试使用开放式 CV 和 SDL 追踪内存泄漏
【发布时间】:2011-09-29 16:38:35
【问题描述】:

下面的程序主循环是我无法找到内存泄漏的地方。我运行 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 字节在 182 的损失记录 180 中仍然可达 1 个块

在 0x4026864: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,440,020 字节在 182 的丢失记录 181 中可能丢失了 1 个块

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

通过 0x415D0EB:cv::fastMalloc(unsigned int)(在 /usr/local/lib/libopencv_core.so.2.3.1 中)

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

67,108,864 字节在 182 的丢失记录 182 中可能丢失了 4 个块

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

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

【问题讨论】:

    标签: linux opencv sdl cups


    【解决方案1】:

    好吧,至少你永远不会在atr上打电话给SDL_FreeSurface()

    【讨论】:

    • 感谢您的帮助 - 我确实在退出时调用它,但您的意思是每次使用 IMG_load 之前我都需要释放表面吗?这只是主循环和其中的一个调用。我需要释放它并为每个负载重新定义它。这是你的建议吗?
    • 我是说IMG_Load() 分配了一个SDL_Surface 并希望您在完成后分配SDL_FreeSurface()
    • 好的,所以我不应该对同一个变量执行 IMG_Loads。我应该加载到一个临时变量,然后将其复制到那里并释放临时表面 - 你是说我调用的每个 IMG_Load 都会生成一个新表面。当我用 IMG_Load 调用同一个变量时,我失去了释放前一个变量的能力,这肯定是一个泄漏。
    • SDL_Surface* atr = IMG_Load("FH_08.jpg"); DrawImage(atr, 0,0,DSP_WIDTH,DSP_HEIGHT,screen, 0, 0, 255); SDL_FreeSurface(atr);但是我仍然在失去记忆,这似乎没有多大帮助。我确信它确实有所帮助,我对此表示感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2012-03-17
    • 2011-02-15
    • 2012-12-21
    相关资源
    最近更新 更多