   Memory leak problems with surfaces in Direct3D
  I'm relatively new to direct3D, after about a year with xna I started on directx and have been working with it for about 6 months, I just finished programming a deffered rendering system with HDR and all sorts of bells and whistles and have started looking at the whole device lost/reset issue.I turned on the directx debugging mode and all that for visual studio and its telling me I have memory leaks when I end the program and whenever the application loses the device and I release all the objects, I can't reset device due to some objects not being released etc.Upon further investigation I have found out that every IDirect3DSurface9* I have in all my code, except for the backbuffer created by directx, doesn't ->Release() for some reason.I wrote a simple program outside of this very complex deffered rendering system to test the behaviour of IDirect3DSurface9* and its corresponding IDirect3DTexture9* that would be used for its data in a shader. I found that if I create a texture on the device and create a surface on the device, when I call Release() on them, they will release and there will be no memory leaks and I can reset device within the program etc. However when I call Texture->GetSurfaceLevel(0, &Surface); after having created the two resources at the start of the program, the texture will release but the surface never does and it causes memory leaks and device cannot be reset etc. I tried reversing the order that the objects are Release() but that didn't make a difference. I went through other programs in the samples that come with the Directx sdk and they don't do anything special but they somehow release the surfaces so its driving me crazy! Is there something I'm missing?   
Quote:Is there something I'm missing?Paragraphs.   ;Quote:Original post by phresnelQuote:Is there something I'm missing?Paragraphs.ehaehahahehaehhhhaeae!But back to the topic, you could post your simple program so we can take a look.;  So you made sure that you released the surfaces that you got from textures before the textures were released?Are you completely obeying the laws of IUnknown throught the codebase?Are you familiar with which of the Direct3D methods increment the reference count for you?   ;  I'm not sure if D3D works like this at all, and I'm not able to check right now but are you setting the surface as a render target and so the "device" still owns a reference to the surface?I'm not sure if it works like this, but maybe setting the render targets back to NULL or however you do it will get rid of the reference.If my guess at how it works it wrong, please ignore this post :P   ;  In D3D9 a lot of calls add a reference to the interface. GetSurfaceLevel is one. GetRenderTarget is another. The doc page for these functions says in the remarks section: "Calling this method will increase the internal reference count..."Every time you call such a method you have to have a corresponding Release.   ;  I never addref in my code as it seems unnecessary, I know that direct3D will do it in some of its methods and that it will do it in GetSurfaceLevel(). If I put a Release() for the texture straight after the call and that created an error in the debug runtime "A level of a mip-map has been released more often than it has been add-ref'ed! Danger!!". If I put a Release() for the surface right after instead of the texture, I get an access violation when Releasing later..I never call the render target to draw to in the debugging application as I'm testing its release functionality right now and I'll build on that once this starts working, at the moment everything is much worse off because I put a release on the surface straight after getting the surface level.   