下面的程序主循环是我遇到内存泄漏问题的地方。我运行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
在丢失记录180中仍然可以获得1块中的32,780字节
位于0x4026864:malloc(vg_replace_malloc.c:236)
by 0x4365BA7:??? (在/usr/lib/libcups.so.2中)
by 0x436731E:ippReadIO(在/usr/lib/libcups.so.2中)
by 0x436785C:ippReadIO(在/usr/lib/libcups.so.2中)
by 0x436785C:ippReadIO(在/usr/lib/libcups.so.2中)
by 0x4367E85:ippRead(在/usr/lib/libcups.so.2中)
by 0x437A173:cupsGetResponse(在/usr/lib/libcups.so.2中)
by 0x437A501:cupsDoIORequest(在/usr/lib/libcups.so.2中)
by 0x437A6FA:cupsDoRequest(在/usr/lib/libcups.so.2中)
by 0x4358386:??? (在/usr/lib/libcups.so.2中)
by 0x4359D52:cupsGetDests2(在/usr/lib/libcups.so.2中)
by 0x435A1B4:cupsGetDests(在/usr/lib/libcups.so.2中)
1个街区中的1,440,020字节可能会在182个丢失记录中丢失
位于0x4026864:malloc(vg_replace_malloc.c:236)
by 0x415D0EB:cv :: fastMalloc(unsigned int)(在/usr/local/lib/libopencv_core.so.2.3.1中)
by 0x4A5DE36 :(主要在下面)(libc-start.c:226)--- DrawImage是主要的唯一东西
4个街区中的67,108,864字节可能会在182个丢失记录中丢失
位于0x4026864:malloc(vg_replace_malloc.c:236)
by 0x473C057:_capture_V4L2(CvCaptureCAM_V4L *,char *)(在/usr/local/lib/libopencv_highgui.so.2.3.1中)
答案 0 :(得分:1)
好吧,至少你永远不会在SDL_FreeSurface()
上致电atr
。