• Advertisement
Sign in to follow this  

(C++)Direct3D9: (WARN) :Memory still allocated! Alloc count = 160

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

The code posted below is based form CodeSampler screenshot app sample witch also gives same warning. When i press key for taking screenshot <pseudo, after device->Present()> if(key S pressed) { lock key S; // just one at a time take screenshot; } and exit app i got this warning:
Quote:
The thread 'Win32 Thread' (0xd80) has exited with code 0 (0x0). Direct3D9: (INFO) :MemFini! Direct3D9: (WARN) :Memory still allocated! Alloc count = 160 Direct3D9: (WARN) :Current Process (pid) = 00000d6c BOMA.exe has triggered a breakpoint The program '[3436] BOMA.exe: Native' has exited with code 0 (0x0).
it says that memory is still allocated, but when i exit without taking screenshot there is no warning so my guess its have to do something in my screenshot class:
#include "CScreenShot.h"

CScreenShot::CScreenShot(LPDIRECT3DDEVICE9 pDevice)
{
	D3DDISPLAYMODE       DisplayMode;
	pDevice->GetDisplayMode(0, &DisplayMode);
	pDevice->CreateOffscreenPlainSurface(
		DisplayMode.Width, DisplayMode.Height,
		D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_ScreenShotSurface, NULL);
}

CScreenShot::~CScreenShot()
{
	if(m_ScreenShotSurface != NULL)
      {
           m_ScreenShotSurface->Release();
           m_ScreenShotSurface = NULL;
      }
}

void CScreenShot::SaveScreenShot(LPDIRECT3DDEVICE9 pDevice)
{
	pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_ScreenShotSurface);
    D3DXSaveSurfaceToFile(L"screenshot.jpg", D3DXIFF_JPG, m_ScreenShotSurface, NULL, NULL);
}
and in my main app class app class header
...
<declaration>
CScreenShot*     m_ScreenShot;
...
in app class cpp init function <definition>
...
m_ScreenShot = new CScreenShot(m_pDevice->GetDevice());
...
in app class destructor
...
delete m_ScreenShot;
...
so i was assumed that everything is freeed propertly, but it is not, and i cant see where i am mistaken. I think i posted all relevant code. Can someone tell me where i am wrong or show me what your code for screenshot looks like?

Share this post


Link to post
Share on other sites
Advertisement
The first thing that stands out to me is the call to GetBackBuffer() - you need to Release() any COM pointer handed to you from a Get*() call, because GetBackBuffer() will increment the reference count on the backbuffer.

Because the backbuffer references the device, the device won't be destroyed either, and all sorts of other things won't either. Things like this tend to be caused by just one or two leaks.

Share this post


Link to post
Share on other sites
You need to delete the call to CreateOffscreenPlainSurface - you don't need it. The reason is that the pointer to that surface is getting overwritten by the call to GetBackBuffer.

In addition you also need to move the call to Release() because every surface returned from GetBackBuffer needs releasing.

See http://msdn2.microsoft.com/en-us/library/bb174379.aspx

This should fix the leak:


#include "CScreenShot.h"

CScreenShot::CScreenShot(LPDIRECT3DDEVICE9 pDevice)
{
m_ScreenShotSurface = NULL;
}

CScreenShot::~CScreenShot()
{
}

void CScreenShot::SaveScreenShot(LPDIRECT3DDEVICE9 pDevice)
{
pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_ScreenShotSurface);
D3DXSaveSurfaceToFile(L"screenshot.jpg", D3DXIFF_JPG, m_ScreenShotSurface, NULL, NULL);
if(m_ScreenShotSurface != NULL)
{
m_ScreenShotSurface->Release();
m_ScreenShotSurface = NULL;
}
}

Share this post


Link to post
Share on other sites
Thanks everyone.

Quote:
The first thing that stands out to me is the call to GetBackBuffer() - you need to Release() any COM pointer handed to you from a Get*() call, because GetBackBuffer() will increment the reference count on the backbuffer...


yes i have read earlier about GetBackBuffer, but i think i need to release
objects that are from Create*() call also. And i had one objects in both functions witch probably cause the leak:). Thanks again i will try Adam_42 fix.

Share this post


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

  • Advertisement