简体   繁体   English

Picturebox上C#Winforms应用程序中的内存泄漏

[英]Memory leak in C# winforms app on picturebox

my application reads frames from a video (does some processing) and then displays it in a picture box - this is done around 10 times a second and i have now ran into a problem where the picturebox image is not being disposed of correctly. 我的应用程序从视频中读取帧(进行一些处理),然后将其显示在图片框中-每秒大约完成10次,现在我遇到了一个问题,即无法正确处理图片框图像。

I get a memory leakage and eventually an out of memory exception. 我遇到内存泄漏,最终出现内存不足异常。

   //Check if PictureBox already contains an image, if so dispose of it.
if (VideoDisplay.Image != null)
{
    VideoDisplay.Image.Dispose();
}
   //set parameter to the new image
displayImage = OverlayImage.UpdateImage(eventArgs.Frame, WarningText, WarningBrush);
   //set the picturebox to the new image
VideoDisplay.Image = displayImage;
   //dispose of brush & start garbage collector
WarningBrush.Dispose();
GC.Collect();

By commenting out the VideoDisplay.Image = displayImage line the memory leakage stops (but obviously i get no image). 通过注释掉VideoDisplay.Image = displayImage行,内存泄漏停止了(但显然我没有图像)。

Just wondered if anyone could give me a hand with this as i have never before worked with disposing of objects etc and dont know if i have made a mistake in disposing or have missed something out. 只是想知道是否有人可以帮我解决这个问题,因为我以前从未处理过物体等,也不知道我在处理时是否犯了错误或错过了什么。

Found the answer turned out by removing the VideoDisplay.Dispose() it fixed it. 通过删除VideoDisplay.Dispose()找到了答案,并对其进行了修复。 By having a quick look up on this there may have been a lock put on the object which the GC cannot then collect. 通过快速查找,可能已锁定了GC无法收集的对象。

Before you update image, you need to dispose it. 在更新映像之前,您需要处理它。 I had similar problem. 我有类似的问题。
Like so: 像这样:

VideoDisplay.Image.Dispose();

After that line of code, update the image. 在那行代码之后,更新映像。 For your scenario: 对于您的方案:

VideoDisplay.Image = displayImage;

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

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