呼唤字体的Dispose()的Windows窗体 [英] Calling dispose() for Font in Windows form
问题描述
我已经运行到同一问题作为的这个问题。也就是说,Fortify的抱怨创建字体对象,为此,新字体()
语句在designer.cs文件中生成的代码。当分析结果看我得到我需要为此做些什么,至少在某些情况下,impreesion。
这是当然的,问题仅在develper已指派他的选择字体的形式在图形设计师。我的计划是要撤消选择和调用的InitializeComponent()
后手动创建字体,然后调用的Dispose()
的字体在出售形式()
方法。既然这样,我做的创建自己的资源(并检查其是否在其他地方使用)我敢肯定它是不是共享的,可安全地允许它的处置。到这一点,我相当有信心的问题。
形式现在的Dispose()
方法创建并有一个布尔处理
参数的形式时,产生为好。什么我不确定(这是我的问题)是我能不能忽略此参数,或在调用之前,检查其是否真的还是假的 font.dispose()
。 (我不得不承认我还不了解这些生成的WinForms的Dispose逻辑)
更具体地说:生成的代码如下:
///<总结>使用
///清理的任何资源。
///< /总结>
///< PARAM NAME =处理>真要是托管资源应释放;否则为false< /参数>
保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
components.Dispose();
}
base.Dispose(处置);
}
假设为形式的'myForm会'的字体受到影响,我想创建它使用
...
的InitializeComponent(); //这是生成到构造函数中
this.myForm.Font =新System.Drawing.Font(NiftFontName,...);
...
和会叫 this.myForm.Dispose ()
处置它。现在的问题是,凡在形式的的Dispose()
方法本应该去。
现在的问题是,凡在形式'dispose()方法这个应该走了。
块引用>
下面是微软一个简单的规则:
受保护的虚拟无效的Dispose(BOOL处置)
{
如果(处置){
//这里没有任何管理对象。
}
//这里免费所有非托管对象。
}
阅读的这个文档了解更多详情。
所以,如果您的字体是从字体文件加载,例如,那么它是不受管理的,你应该处理它
如果(处置)
块外。I've been running into the same issue as described in this question. That is, Fortify complained about the creation of font objects, for which the
new Font()
statements are generated code in the designer.cs files. When looking at profiling results I do get the impreesion I need to do something about this, at least in some cases.This is, of course, an issue only if the develper has assigned a font of his choice to the form in the graphical designer. My plan is to undo that choice and to create the Font manually after the call to
InitializeComponent()
and then calldispose()
for the Font in thedispose()
method of the form. Since that way I do create the resource myself (and checked whether it is used elsewhere) I'm sure it is not shared and can safely allow it's disposal. Up to that point I'm rather confident about the issue.Now the
dispose()
method of the form is generated as well when the form is created and has a booleandisposing
parameter. What I'm uncertain about (and this is my question) is whether I can ignore this parameter, or have to check it for true or false before callingfont.dispose()
. (I have to admit I do not yet understand the dispose logic in these generated winforms).More specifically: the generated code looks as follows:
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
Assuming a font for form 'myForm' is affected, I'd create it using
... InitializeComponent(); // this is generated into the constructor this.myForm.Font = new System.Drawing.Font("NiftFontName",...); ...
and would call
this.myForm.Dispose()
to dispose it. The question is, where in the forms'dispose()
method this should go.解决方案The question is, where in the forms' dispose() method this should go.
Here is a simple rule from Microsoft:
protected virtual void Dispose(bool disposing) { if (disposing) { // Free any managed objects here. } // Free any unmanaged objects here. }
Read this documentation for more details.
So if your font is loaded from a font file, for instance, then it is unmanaged and you should dispose it outside the
if (disposing)
block.这篇关于呼唤字体的Dispose()的Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!