Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CFont and DeleteObject()

250 views
Skip to first unread message

James Juno

unread,
Jun 5, 2008, 11:55:40 AM6/5/08
to
Why does MSDN tell me to explicitly call DeleteObject() on a CFont
object but doesn't make the same requirement for other types of GDI
objects? I've gotten in the habit of always calling DeleteObject()
because I don't understand the scope of this inconsistency.

Much thanks,
JJ

AliR (VC++ MVP)

unread,
Jun 5, 2008, 12:06:57 PM6/5/08
to
Where does it say to explicity call DeleteObject on a CFont? I can't find
it.

AliR.


"James Juno" <j...@asteroid.fake> wrote in message
news:X46dnQZtG5rgkdXV...@scnresearch.com...

James Juno

unread,
Jun 5, 2008, 12:12:30 PM6/5/08
to
Most of the member function descriptions involving font creation either
explicitly state it or otherwise show it in their example snippets. For
instance,

http://msdn.microsoft.com/en-us/library/2ek64h34(VS.71).aspx

"When you finish with the CFont object created by the CreateFont
function, first select the font out of the device context, then delete
the CFont object."

-JJ

AliR (VC++ MVP)

unread,
Jun 5, 2008, 12:25:10 PM6/5/08
to
I guess that for demonstration purposes. Obviously you can call
DeleteObject and then create a new font using the same variable.
But you don't have to call DeleteObject, as CFont inherits from CGdiObject
and it's destructor calls DeleteObject.

AliR.


"James Juno" <j...@asteroid.fake> wrote in message

news:VIOdnTBN6LjyjdXV...@scnresearch.com...

Doug Harrison [MVP]

unread,
Jun 5, 2008, 12:22:16 PM6/5/08
to
On Thu, 05 Jun 2008 09:12:30 -0700, James Juno <j...@asteroid.fake> wrote:

>Most of the member function descriptions involving font creation either
>explicitly state it or otherwise show it in their example snippets. For
>instance,
>
>http://msdn.microsoft.com/en-us/library/2ek64h34(VS.71).aspx
>
>"When you finish with the CFont object created by the CreateFont
>function, first select the font out of the device context, then delete
>the CFont object."

As CFont is derived from CGdiObject, DeleteObject will be called on the GDI
font when the CFont object is destroyed. You don't need to call
DeleteObject yourself unless you want to delete the GDI font before the
CFont is destroyed.

--
Doug Harrison
Visual C++ MVP

James Juno

unread,
Jun 5, 2008, 12:44:31 PM6/5/08
to
Thanks. Same is true of any CGdiObject, so I guess I was confused why
CFont was singled out in the documentation.

James Juno

unread,
Jun 5, 2008, 12:47:56 PM6/5/08
to

Thanks. Although it doesn't hurt anything, I can stop making redundant
calls to DeleteObject() on CFont objects. I've been doing that for
years. ;)

-JJ

Joseph M. Newcomer

unread,
Jun 7, 2008, 11:32:17 PM6/7/08
to
For example:

{
CFont f;
f.CreateFont(...);
CFont * old = dc.SelectObject(&f);
...
dc.SelectObject(old);
} // <== CFont object is deleted here

Why did you assume that "delete the CFont object" had anything to do with "Call
DeleteObject on the CFont"?
joe

On Thu, 05 Jun 2008 09:12:30 -0700, James Juno <j...@asteroid.fake> wrote:

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages