简体   繁体   English

内存泄漏,大量位图图像C ++ CLI

[英]Memory leak, numerous bitmap images C++ CLI

I'm getting an out of memory error. 我遇到内存不足的错误。 I have one form in a picture-processing app. 我在图片处理应用程序中有一种形式。

Here is the relevant portion of code from a void routine: 这是void例程中代码的相关部分:

Image^ CurrentImage;
Image^ ProcessedImage;
for(int i=0; i<NumSourceFiles; i++) {
   CurrentImage = Image::FromFile(File[i]);
   ProcessedImage = ProcessImage(CurrentImage, ShrinkToWidth, ShrinkToHeight, Rectangle(LeftOrigin,TopOrigin,CropWidth,CropHeight));
   NumFiles++;  //increment to get next number to save
   NextFileName = folderName_specific+"\\"+SelectedCondition+"_"+Convert::ToString(NumFiles)+".jpg";
   ProcessedImage->Save(NextFileName, ImageFormat::Jpeg);
   delete CurrentImage;
   delete ProcessedImage;
}

Now here is the ProcessImage routine: 现在,这里是ProcessImage例程:

private: Image^ ProcessImage(Image^ img, int width, int height, Rectangle cropArea)  //shrink and crop
{
   Bitmap^ bmpImage = gcnew Bitmap(img);
   bmpImage = bmpImage->Clone(cropArea, bmpImage->PixelFormat);
   Bitmap^ bmpImage1 = gcnew Bitmap(bmpImage, width, height);
   delete bmpImage;
   return bmpImage1;
}

If I get rid of all the delete statements, then it will process around 200 images before running out of memory. 如果我删除了所有delete语句,那么它将在内存不足之前处理大约200张图像。 If I remove the two delete statements in the void routine it will process about 300. If I keep them all in it will do around 500 images before an out of memory failure. 如果我在void例程中删除了两个delete语句,它将处理大约300个。如果将它们全部保留在其中,它将在内存不足故障之前处理大约500张图像。 It always fails at the first statement of the ProcessImage routine. 它总是在ProcessImage例程的第一条语句处失败。

I tried making bmpImage1 a global variable and deleting it in the void routine, but that didn't help. 我尝试将bmpImage1设置为全局变量,并在void例程中将其删除,但这无济于事。 Any idea where I'm building up memory? 知道我在哪里建立内存吗?


Update 12April2018: I simplified this as follows (stuck it all in the for/next loop) and it STILL runs out of memory after 550 or so pictures are saved. 更新2018年4月12日:我将其简化如下(将其全部保留在for / next循环中),并且在保存550张左右的图片后仍会耗尽内存。 Stumped.... Really need to solve this... Lil' help? 难过....真的需要解决这个问题吗? (Pretty please with sugar on top!) (请在上面放糖!)

for(int i=0; i<NumSourceFiles; i++) {
    Image^ CurrentImage = Image::FromFile(File[i]);
    NumFiles++;  //increment to get next number to save
    NextFileName = folderName_specific+"\\"+SelectedCondition+"_"+Convert::ToString(NumFiles)+".jpg";
    Bitmap^ bmpImage = gcnew Bitmap(CurrentImage);
    bmpImage = bmpImage->Clone(Rectangle(LeftOrigin,TopOrigin,CropWidth,CropHeight), bmpImage->PixelFormat);
    Image^ ProcessedImage = gcnew Bitmap(bmpImage, ShrinkToWidth, ShrinkToHeight);
    ProcessedImage->Save(NextFileName, ImageFormat::Jpeg);
    delete bmpImage;
    delete ProcessedImage;
    delete CurrentImage;
}

Solved it by adding these two statements after the three delete statements: 通过在三个delete语句之后添加以下两个语句来解决该问题:

GC::Collect(); GC :: Collect(); GC::WaitForPendingFinalizers(); GC :: WaitForPendingFinalizers();

You must use both statements in this order. 您必须按此顺序使用两个语句。 Memory leak vanquished. 内存泄漏消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM