   Setting back buffer - memory leak
  Hi, I've been working on a deffered renderer and have hit a small snag. I've been doing a lot of render target setting, but when I try and render to the back buffer, I get a memory leak even when I release the surface after using it. Here's what I'm doing:HRESULT Render(){HRESULT hr;g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pSurfScene);// start rendering the sceneg_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, 1.0f), 1.0f, 0);if(SUCCEEDED(g_pd3dDevice->BeginScene())){SetGBuffer();ClearGBuffer();RenderGBuffer();ResolveGBuffer();DefferedDirectionalLight();g_pd3dDevice->EndScene();}else return E_FAIL;g_pd3dDevice->Present(NULL, NULL, NULL, NULL);   SAFE_RELEASE(g_pSurfScene);return hr;}void SetGBuffer(){g_pd3dDevice->SetRenderTarget(0, g_pSurfaceColour);g_pd3dDevice->SetRenderTarget(1, g_pSurfaceNormal);g_pd3dDevice->SetRenderTarget(2, g_pSurfaceDepth);}void ResolveGBuffer(){g_pd3dDevice->SetRenderTarget(0, g_pSurfScene);g_pd3dDevice->SetRenderTarget(1, NULL);g_pd3dDevice->SetRenderTarget(2, NULL);}There's more code, but that is all that is concerned with setting render targets. It renders to the screen fine, but there's a memory leak which I'm not sure how to fix.Thanks   
  That's a pretty vague description of the problem and you didn't say how you determined that you think there's a memory leak.What, exactly, makes you think this?   ;  If I remove the line:g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pSurfScene);I don't get a memory leak.   ;  Try releasing the surface before returning E_FAIL at the "else".Are you sure you're not addRef()-ing it anywhere else?   ;  The problem is that you're getting the back buffer every time you render, but as another poster pointed out, you have code paths that don't release the back buffer.You're making life hard on yourself by making all your variables global.  You're not going to get the automatic lifetime management of scoped variables and smart pointers by doing this.  You're forcing yourself to manage each reference count explicitly, instead of letting the normal scoping rules of the compiler do it for you.If you use a smart pointer, then no matter how you get out of the Render() function, the interface pointer is going to be released.See Direct3D Programming Tip #3: Use Smart Pointers from my blog.  If you're on the express edition of VS, then use _com_ptr<T> instead of CComPtr<T>.Also, hr is never initialized, so your Render() function may return uninitialized garbage.Honestly, the whole business of that SAFE_RELEASE macro hides more problems than it solves by repeatedly calling Release instead of making sure that each call to Release is matched by a corresponding AddRef (which may be done on your behalf by a function that returns an interface pointer, like GetBackBuffer).   ;  By the looks of the smart pointers proposed in your blogs, I would require the ATL libraries? Is this the case?Thanks   