我收到内存不足错误。我在图片处理应用程序中有一个表单。
以下是来自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;
}
现在这里是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;
}
如果我删除所有删除语句,那么它将在耗尽内存之前处理大约200个图像。如果我删除了void例程中的两个delete语句,它将处理大约300个。如果我将它们全部保存在其中将在内存不足故障之前执行大约500个图像。它始终在ProcessImage例程的第一个语句中失败。
我尝试将bmpImage1作为一个全局变量并在void例程中删除它,但这没有帮助。知道我在哪里建立记忆吗?
更新12April2018: 我将其简化如下(将它全部放在for / next循环中)并且在保存550张左右的图片后STILL会耗尽内存。难倒......真的需要解决这个问题...... Lil'有帮助吗? (相当喜欢加糖!)
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;
}
答案 0 :(得分:0)
通过在三个删除语句之后添加这两个语句来解决它:
GC ::收集(); GC :: WaitForPendingFinalizers();
您必须按此顺序使用这两个语句。记忆泄漏已经消失。