Sign in to follow this  
Brejlounek

GetFrontBufferData does memory leak

Recommended Posts

Brejlounek    169
Hi there, I make an app which repetadly uses screenshot image data. I am using GetFrontBufferData but I have one serious problm with it. Whenever I call it, do some changes to the surface and free it, system memory used by my app grows a little. I am sure I am releasing everything properly, it surely does GetFrontBufferData. At start, my app uses about 37 MB of memory. After few tens of GetFrontBufferData it reaches a number near 51 MB and program crashes - with an error E_OUTOFMEMORY. The strange thing is there is no full memory, nothing is exceeded - in a moment, when my app uses about 51 MB there is 2 GB out of 4 GB system memory free and 230 MB out of 512 MB video memory free. I don't get it. Can you help me, please? Where could this error be?

Share this post


Link to post
Share on other sites
unbird    8338
Can you show us the relevant code, please (retrieving and releasing the surface and also the changes you mentioned) ?

If you haven't tried already: Comment out code which you believe is responsible and check if the crash persists. It might be somewhere else.

And have you used PIX ?

Share this post


Link to post
Share on other sites
Brejlounek    169
Thank you for your quick response.

Here's the relevant part of code:

LPDIRECT3DTEXTURE9 o1tex = NULL;
IDirect3DSurface9 *o1surf=NULL;
IDirect3DSurface9 *o1texs=NULL;

if (!dr(DevView->CreateOffscreenPlainSurface(1680,1050,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&o1surf, NULL),L"o1surf error")) //main surface
{
return;
}

if (!dr(DevView->CreateOffscreenPlainSurface(1680,1050,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&o1texs, NULL),L"o1texs error")) //texture surface
{
return;
}

if (!dr(D3DXCreateTexture(DevView,1680,1050,1,0,D3DFMT_X8R8G8B8,D3DPOOL_MANAGED,&o1tex),L"o1tex error")) //texture, DevView is d3d device
{
return;
}

dr(DevView->GetFrontBufferData(0,o1surf),L"Error"); //dr captures a dx error

//locking texture surface and do a simple change to see if it works
D3DLOCKED_RECT pu;
o1surf->LockRect(&pu,0,0);
BYTE* pBits;
pBits = (BYTE*)pu.pBits;

int index;
for (int y=0;y<vdisphe;y+=1)
for (int x=0;x<vdispwi;x+=1)
{
index= (x*4)+(y*pu.Pitch);
pBits[index]=255-pBits[index];
pBits[index+1]=255-pBits[index+1];
pBits[index+2]=255-pBits[index+2];
}

o1surf->UnlockRect();

o1tex->GetSurfaceLevel(0,&o1texs); //get texture surface
D3DXLoadSurfaceFromSurface(o1texs,NULL,NULL,o1surf,NULL,NULL,D3DX_FILTER_LINEAR,0); //copy o1surf into o1texs

//now I render the texture, not important

//releasing
SAFE_RELEASE(o1surf);
SAFE_RELEASE(o1tex);
SAFE_RELEASE(o1texs);


If I uncomment a line with GetFrontBufferData, surface is blank (of course) but memory usage is still. It's really GetFrontBufferData.

I did never use PIX before but it sounds like a very useful utility. I'll try using it soon.

Share this post


Link to post
Share on other sites
c3Dp    140
Huh,

the 2nd surface (the DevView->CreateOffscreenPlainSurface(1680,1050,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&o1texs, NULL),L"o1texs error") ) isn't freed and never used actually.


o1tex->GetSurfaceLevel(0,&o1texs) overwrites o1texs value, so your pointer is lost.

Share this post


Link to post
Share on other sites
unbird    8338
Nice catch c3Dp. Right, no need to create the surface beforehand, GetSurfaceLevel will actually return one.

Quote:
If I uncomment a line with GetFrontBufferData, surface is blank (of course) but memory usage is still. It's really GetFrontBufferData.


(I think you meant "comment out"). Did you really comment out only that line ?

Share this post


Link to post
Share on other sites
Brejlounek    169
Yes, I mean comment out, sorry.

Thank you, c3Dp! That was the problem, I deleted o1texs inicialization so now I free o1texs from GetSurfaceLevel properly. The problem probably disappeared by commenting out GetFrontBufferData because there simply wasn't something that could be loaded into memory.

Thank you! :)

Share this post


Link to post
Share on other sites

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