Remove CFont well


I have a question about CFont. I use in the constructor of my class:

m_comboFont = new CFont();

Where I have "CFont* m_comboFont;" inside the header.

How I have to remove the m_comboFont in the destructor to avoid memory leaks?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Ocrana,

you have to first delete the GDI font and then the CFont, i.e.:

> m_comboFont->DeleteObject();
> delete m_comboFont;

But, before you can do this you have to be sure the font isn't still selected into a device context, otherwise you'll create GID leaks.

Hope that helps,

OcranaAuthor Commented:

my problem is that I get a bad heap crash if I call "delete" with any CFont there.  

AndyAinscowFreelance programmer / ConsultantCommented:
>>How I have to remove the m_comboFont in the  destructor to avoid memory leaks?

Just a general point, ALL memory allocated with 'new' must be deallocated with 'delete' to prevent memory leaks.  
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Probably you create this font somewhere in OnInitDialog or OnCreate. So you need to add an OnDestroy/OnDestroyWindow method and release such objects there.

You cannot delete any GDI object (the font in your case) if it is selected somewhere (you set this manually created font to a combo box). So firstly you need set a default font and the you call delete for your CFont object (DeleteObject should be called from the CFont destructor, if I remember correctly).

I propose you to ignore all this story and add a member to your window class, for example, CFont m_comboBoxFont (without *). So you will not need to write new and delete for this object and everything will work fine.
>> my problem is that I get a bad heap crash if I call "delete" with any CFont there.  

That's strange - could it be that you already delete the CFont anywhere else?

BTW: In fact the 'm_comboFont->DeleteObject();' isn't needed since it should be impliciteley called from CFont destructor.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
May be you have also a local declartion of CFont* m_comboFont ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.