Sign in to follow this  

[C++] Obscure memory leak.

This topic is 2843 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

D3DX complains of a memory leak if I call this small method.
bool Font::Define( D3DDevice dev, const tstring& facename, cuint height )
{
	// Validate parameters.
	if ( !dev || facename.empty() ) return false;

	// Wipe out previous font
	m_pFont = 0;

	// Return if the font was created successfully.
	return S_HR(D3DXCreateFont(dev,height,0,0,0,false,DEFAULT_CHARSET,0,0,0,facename.c_str(),&m_pFont));
}
I do nothing else with the object in question, nothing is dynamic, and a perfectly functional Smart COM pointer is at work with the m_pFont = 0 statement and D3D device parameter. I've used the same object for nearly every COM interface I can think of without issue. Bottom line: If I call this method, I get a memory leak. If not, I don't. Again, nothing else in the Font scope is used at all. I tried to use the BreakOnAllocID property in the control panel. I know it's decimal there, but I tried both possible values anyway. It didn't break. What on earth is going on?

Share this post


Link to post
Share on other sites
Are you sure your code is setup to ensure the font is released before the device is?

edit: and is D3DDevice supposed to be copy by value or pass-by-reference?

Share this post


Link to post
Share on other sites
Quote:
setting it to 0 won't simply release the memory. isn't there a Release() method for font?


Yes, but again, smart pointer at work. It automates AddRef() and Release(), and a 0 assignment would release the memory.

Quote:
edit: and is D3DDevice supposed to be copy by value or pass-by-reference


In this case, copy.
inline ComPtr( T* c ): P(c) { if (P) P->AddRef(); }


Quote:
Are you sure your code is setup to ensure the font is released before the device is?


I am. The device is released by my app object, which is the very first thing put on the stack. Everything else is freed by the time the device is ready to meet it's maker.

Due to the fact this is an isolated incident, (similar code is everywhere) I won't rule it out.

Share this post


Link to post
Share on other sites
I managed to solve my problem. I had several bugs fixed at once, and I have to admit I don't know which one fixed this since a couple of them provoked undefined behavior.

To answer your question, S_HR works into the following.

// Error Router: Couples with HR macro to 
// report errors to the developer.
inline HRESULT IDXErrorRouter( CHAR* file, DWORD line, const HRESULT hr, TCHAR* msg )
{
return FAILED(hr) ? DXTrace( file, line, hr, msg, true ) : hr;
}

/*******************************************************************************
Macro: HR
Pass HRESULT to imply call to IDXErrorRouter.
On release builds, this merely expands to the
passed symbol.
*******************************************************************************/


#define HR(x) IDXErrorRouter( __FILE__, __LINE__, x, TEXT(#x) )

#define S_HR(x) SUCCEEDED(HR(x))

Share this post


Link to post
Share on other sites

This topic is 2843 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this