本文介绍了HFONT&上的SelectObject GDI资源泄漏.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(添加了更多内容)
你好''

关于SelectObject的一个小问题(包括Win32,而不是CDC).

我正在审查一大段绘制文本的代码,我们在调用SelectObject(fontHandle)之后查询文本metrix,但我们从不保留旧对象,也从未恢复过旧句柄:
(1)

(added more stuff )
Howdy''

Small question regarding SelectObject (mosly the Win32 one, not the CDC one).

I''m reviewing a large piece of code that draws text and we query the text metrix after calling SelectObject( fontHandle) but we never keep the old object and never restore the old handle:
(1)

{
 /// ... some code...
 ::SelectObject( validHDC, validHFONT );
 /// ... do something ...
}



通常我习惯看到像
这样的seomthing(2)



normally I''m used to seeing seomthing like
(2)

void f{)
{
 /// ... some code...
 HFONT oldFont = ::SelectObject( validHDC, validHFONT );
 /// ... do something ...

 ::SelectObject( validHDC, oldFont);
}





如果我在代码中有成千上万个SelectObject时执行(1)而不是(2),是否会引起很多问题(资源泄漏和/或性能)?

最后,当我在同一函数中有多个SelectObject时,首选的方法是什么?我是否只需要保留第一个SelectObject的旧句柄并在最后将其重置?





Will that cause a lot of issues (resource leaks and/or performance) if I do (1) instead of (2) when there are gazillion SelectObject in the code ?

and finally, what is the preferred way to do it when I have multiple SelectObject in the same function? do I only need to keep the old handle for the first SelectObject and reset it at the end ?

void f{)
{
 /// ... some code...
 HFONT oldFont = ::SelectObject( validHDC, validHFONT );
 /// ... do something ...
 ::SelectObject( validHDC, anotherHFONT );
 /// ... do something ...

 ::SelectObject( validHDC, thirdHFONT );
 /// ... do something ...

 ::SelectObject( validHDC, fourthHFONT );
 /// ... do something ...

 ::SelectObject( validHDC, oldFont);
}




谢谢.
最多




Thanks.
Max.

推荐答案


这篇关于HFONT&上的SelectObject GDI资源泄漏.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:31