   D3D Memory Leak.. with smart pointers?
  Hello All,The past day or two I''ve been debugging my app, which has four memory leaks, which I''ve found out point to the creation of the D3D object (not the device), using the "break on allocid" field in the control panel directx dialog.  This would lead me to believe that I am not releasing the Direct3D object, or else another object associated with the Direct3D object hasn''t been released. The strangest thing is that I use smart pointers for all of my COM interfaces.I tried changing the type of both the D3D9 object and D3DDevice9 object to regular long pointers, and with the proper release code added in, the memory leaks go away.  So, now I know that it isn''t something else that''s causing the memory leak, just the smart pointers themselves (or something I am not doing with the smart pointers).  I was under the impression that with smart pointers, I didn''t have to do any cleanup.  Is there some reason that I''m receiving the memory leak, and is there anyway to solve the problem?  I''ll provide more information as necessary.Thanks in advance!  ___  ___  ___  ___  ___  ___  ___  ___ /\  \/\__\/\  \/\  \/\  \/\  \/\  \/\__\    /::\  \   /:/ _/_   _\:\  \   _\:\  \    \:\  \   /::\  \   /::\  \   /:/ _/_  /::\:\__\ /:/_/\__\ /::::\__\ /::::\__\   /::\__\ /::\:\__\ /::\:\__\ /::-"\__\ \/\:\/__/ \:\/:/  / \::;;/__/ \::;;/__/  /:/\/__/ \;:::/  / \:\:\/  / \;:;-",-"    \/__/   \::/  /   \:\__\    \:\__\    \/__/|:\/__/   \:\/  /   |:|  |\/__/\/__/\/__/\|__|\/__/\|__|  
  Anyone have any ideas?  I can provide code if needed, though I''m not exactly sure it would help. ;  AFIAK the smart pointers should automatically call release when they go out of scope. Are you sure they are going out of scope?;quote:Original post by Fuzztrek  This would lead me to believe that I am not releasing the Direct3D object, or else another object associated with the Direct3D object hasn''t been released.My experience is that whenever a d3d resource is not released, the d3d object is not released (that''s because - according to my observations - when the object creates the device, it increments its own reference count. And when the device creates a resource, it increments its own reference count, ...etc. So when there''s on leak, everything in the chain remains unreleased).quote:The strangest thing is that I use smart pointers for all of my COM interfaces.I tried changing the type of both the D3D9 object and D3DDevice9 object to regular long pointers, and with the proper release code added in, the memory leaks go away.  So, now I know that it isn''t something else that''s causing the memory leak, just the smart pointers themselves (or something I am not doing with the smart pointers).  I was under the impression that with smart pointers, I didn''t have to do any cleanup.  Is there some reason that I''m receiving the memory leak, and is there anyway to solve the problem?  I''ll provide more information as necessary.Are you sure you''re using the smart pointers properly?Write a small test app using smart pointers, and see if it produces a leak - then search for your mistake in using the smart pointers.Nice sig, by the way.   Muhammad HaggagMHaggag''s corner;  I'm not sure if I'm using the smart pointers correctly.. as far as I knew all I had to do was change this:IDirect3D9 * m_D3D;to this:ATL::CComPtr<IDirect3D9> m_D3D;The only other thing different that I can think of is that I don't have any release and null code for the pointers.  Is there something else I should be doing, so some way to figure out why they aren't cleaning up after themselves?My other smart pointers don't seem to have a problem... I use smart pointers for my vertex buffers and textures, and they havn't caused any problems.Thanks in advance! [edited by - Fuzztrek on October 14, 2003 7:08:16 PM];  Make sure you've got the order of release down very carefully.If your IDirect3DDevice9* pointer is released afterthe IDirect3D9* pointer, you're going to get some "memory still allocated" complaints from D3D.EDIT: Thoguh it seems unlikely that that is what's causing your problems. Releasing in the wrong order like that usually has a memory read exception associated with it.   [edited by - glassJAw on October 15, 2003 1:29:58 PM];  I''ve played around with non-smart pointers to d3d devices and objects, however I''ve never found that releasing in the wrong order has caused a memory leak - at least I didn''t receive one when I released the d3d object before the device (I know it''s not a good thing to do.)  However, that may be the problem, and with smart pointers I don''t really think I have any control over which order they release themselves in, but I''ll look into it.  Thanks for replying! ;quote:Original post by glassJAw  Make sure you''ve got the order of release down very carefully.If your IDirect3DDevice9* pointer is released after  the IDirect3D9* pointer, you''re going to get some "memory still allocated" complaints from D3D.EDIT: Thoguh it seems unlikely that that is what''s causing your problems. Releasing in the wrong order like that usually has a memory read exception associated with it.   [edited by - glassJAw on October 15, 2003 1:29:58 PM]Are you sure of this?I''ve written DX9 tests where I release stuff in reverse order, and it worked fine. I''ve not gone far, though, I''ve just released the d3d object before the device and everything worked fine (no leaks).My current understanding is that when the d3d object creates a device, the object''s refcount is incremented. And when the device creates an object (resource), the device''s refcount is incremented.Ditto for when stuff is released. Muhammad HaggagMHaggag''s corner;  [bump]Has anyone else had any problems with smart pointers and memory leaks?  Or can anyone tell me how they use smart pointers with d3d?  Replies greatly appreciated! ;  Hmm.. I guess not.  I may as well just use regular pointers for the device and object then. 