Advertisement Jump to content
Sign in to follow this  

Found memory leak, but don't know how to fix it*solved*

This topic is 4913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using C++ and Visual Studio .NET 2003. I have gotten Fluid Studios Memory Manager to compile in my project and it tells me of a memory leak in my function "Add Font" at the exact line, highlighted in the code below:
//add font
void CInterfaceManager::AddFont(const string &sFontName,const string &sFontFaceName,const int &nFontHeight,const bool &bBold,const bool &bItalic)
	int nFontWeight;

        //struct loaded font is explained full below this function code
	LOADEDFONT *newFont=new LOADEDFONT;//memory leak on this line

	//create font
	HRESULT hr=D3DXCreateFont(g_pD3DDevice9, //D3D Device
                     nFontHeight,                //Font height
                     0,                          //Font width
                     nFontWeight,                //Font Weight
                     1,                          //MipLevels
                     bItalic,                    //Italic
                     DEFAULT_CHARSET,            //CharSet
                     OUT_DEFAULT_PRECIS,         //OutputPrecision
                     ANTIALIASED_QUALITY,        //Quality
                     DEFAULT_PITCH|FF_DONTCARE,  //PitchAndFamily
                     sFontFaceName.c_str(),      //pFacename,
                     &newFont->m_Font);          //ppFont

        //the container type used here is a list
	m_lstFonts.push_back(newFont);//add new font to list of fonts

//struct LoadedFont
    ID3DXFont *m_Font;   //the font
    string m_sFilename;  //the filename of the font
    int m_FontSize;      //size of font

So, now that I have found this memory leak, how do I fix it? The code looks alright to me. [Edited by - utilae on August 9, 2005 11:52:28 PM]

Share this post

Link to post
Share on other sites
Well, where do you delete your newfont pointer? If you never call delete on it then yes, you get a memory leak.

So: Either call delete on newfont before it gets destroyed (such as in the destructor) or use any of the various smart pointer classes that will call delete automatically for you when the pointer gets destroyed. (std::auto_ptr or the boost smart pointers for example)

Share this post

Link to post
Share on other sites
Well, a memory leak is when you allocate memory on the heap and never delete it, even after the variable goes out of scope. So what your manager is telling you is that you are never deleting newFont, or some part of it. I would check this:

Are you deleting the contents of m_lstFonts? I'm just guessing it's a vector. I could be wrong. Also, are you responsible for deleting m_Font after it has had memory allocated to it? If so, is it being deleted? It won't be in the LOADEDFONT destructor by default - you need to specify that yourself by writing your own destructor.

My two cents.

Share this post

Link to post
Share on other sites
Ok, thanks. You're right. I was not deleting it. I did have a function that removed the fonts, etc, but I forgot to add a line to delete the loadedfont struct.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!