Click to See Complete Forum and Search --> : How to destroy a CFont Object that I create


Rose Mary
April 15th, 1999, 09:58 AM
Hello, All.
For a CFont object that I create, I need to destroy the object when I am done using it. Otherwise will cause a space wasted. But what is the right way to destroy the object ?
please see the following case:
CFont NewFont;
CFont *pOldFont;
NewFont.CreateFont();
pOldFont = pDC->SelectObject(&NewFont);
....
pDC->SelectObject(pOldFont);

After I select pOldFont back, Do I need to delete the NewFont and How to ? or the destructor of NewFont will take care of this thing ?

Thanks!

IanH
April 16th, 1999, 10:19 AM
The destructor will take care of this for you, you only need to call the delete function if previously you allocated memory space with 'new' as follows:

CFont *m_pPrintFont;
m_pPrintFont = new CFont;
....
if( m_pPrintFont )
delete m_pPrintFont;



Best regards

Ian

Alvaro
April 16th, 1999, 04:12 PM
Ian is right. The call which releases the font resource is DeleteObject which the CGdiObject destructor will call for you.

For questions like these, don't forget that you can always look in the MFC source code to see what's going on, or better yet, Step Into it as you're debugging.

Regards,

Alvaro