.NET处理使用大量GDI对象的经典UserControls [英] .NET disposing of classic UserControls working with a lot of GDI objects

查看:60
本文介绍了.NET处理使用大量GDI对象的经典UserControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

Good morning,

我正在开发一个自定义UserControls(经典,无WPF)集成在专有自动化软件上,用于设计人机界面。我的UserControls通过使用GDI API绘制了大量图形(位图,阴影,边框,形状......以获得公司风格指南描述的
a自定义界面) - 我不会调用本机GDI函数而只是System.Drawing库提供的包装器。我编译为64位硬件平台,我的目标版本编译
是.NET 4.0(出于兼容性原因)。

I'm developing a collection of custom UserControls (classic, no WPF) to be integrated on a proprietary automation software to design human-to-machine interfaces. My UserControls draw a lot of graphics (bitmaps, shadows, borders, shapes, ... to get a custom interface described by company style guide) by working with GDI APIs - I don't call for native GDI functions but just the wrappers offered by the System.Drawing library. I compile for a 64bits hardware platform and my target version for compilation is .NET 4.0 (for compatibility reasons).

作为一个案例研究,Iet考虑一个位图:当不再引用时,我知道垃圾收集器收集指针并释放它,但是为位图和GDI句柄分配的内存不会自动释放除非你明确
调用位图对象本身的dispose()方法。在运行时,每个UserControl都会在界面上绘制所有内容,并最终处理不再使用的GDI资源。该应用程序是一个多形式程序,因此当一个表单关闭时,所有它的资源
将被处理掉一个新的实例化并显示出来。

As a case study, Iet's consider a bitmap: when not anymore referenced, I know that the garbage collector collects the pointer and releases it, but the memory allocated for the bitmap and the GDI handle is not released automatically unless you explicitly call the dispose() method on the bitmap object itself. While running, each UserControl draws everything on the interface and finally disposes GDI resources not anymore in use. The application is a multiform program, so when a form is closed all it's resources are disposed a new one is instantiated and shown.

我的疑问是:当我的父表单关闭时会发生什么,所以所有的子控件都被处理了?是否在所有位图(以及尚未处置的所有其他GDI对象)上自动调用dispose()方法,以便正确释放分配的
内存?我是否必须通过首先在仍在使用的所有GDI对象上调用dispose然后调用基本方法来覆盖dispose()方法? 

My doubt is: what happens when my parent form is closed, so all the children controls are disposed? Is the dispose() method automatically invoked on all bitmaps (and all the other GDI objects not yet disposed) so correctly freeing the allocated memory? Do I have to override the dispose() method by first calling dispose on all the GDI objects still in use and then calling the base method? 

我试图模拟在这种情况下,通过查看任务管理器为我的进程显示的GDI句柄数量,似乎句柄在表单关闭时正确处理,就像在每个GDI
对象上自动调用dispose()方法一样还在使用中。你能确认一下吗?

I tried to simulate this case and, by looking at the number of GDI handles shown by the task manager for my process, it seems that handles are correctly disposed at form closing, like if the dispose() method is called automatically on every GDI object still in use. Can you please confirm?

我看了一下微软网站上的一些文章:大多数人都说必须通过调用dispose()手动释放GDI资源(Bitmap,Brush,Pen,Graphics,...),否则内存不会真正释放并且GDI句柄不会被释放 -
只是指针被垃圾释放集电极。问题是我在处理图形时没有发现有关控制处理的详细信息,例如我的情况。 

I took a look at some articles on Microsoft website: most people say that GDI resources (Bitmap, Brush, Pen, Graphics, ...) must be released manually by invoking dispose() otherwise memory is not really freed and the GDI handle is not released - just the pointer is released by the garbage collector. The problem is that I found no details about controls dispose when working with graphics like in my case. 

我不想得到任何"通用GDI错误"。由于没有正确释放内存而导致GDI内存泄漏错误...有谁可以帮助我?

I don't want to get any "Generic GDI error" or GDI memory leak error due to not correctly freed memory ... can anyone please help me?

非常感谢。

推荐答案

嗨alebi88,

Hi alebi88,

感谢您在此发帖。

>>问题是我在处理图形时没有发现有关控制处置的详细信息,例如我的情况。 

>>The problem is that I found no details about controls dispose when working with graphics like in my case. 

对于您的问题,如果你担心这个,请尝试使用using语句来释放资源。

For your question, if you are worry about this, please try to use using statement to release resource.

完成资源后,你需要确保调用对象的Dispose方法。 using语句很容易获得。

When you have finished with the resource you need to ensure the object's Dispose method is called. The using statement is easy to get.

 using (Pen dashed_pen = new Pen(Color.Red, 10))
    {
        dashed_pen.DashStyle = DashStyle.Dot;
        e.Graphics.DrawPolygon(dashed_pen, points);
    }

最好的问候,

Wendy


这篇关于.NET处理使用大量GDI对象的经典UserControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆