Archived

This topic is now archived and is closed to further replies.

D3D Memory Leak.. with smart pointers?

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

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!
    ___       ___       ___       ___       ___       ___       ___       ___   
   /\  \     /\__\     /\  \     /\  \     /\  \     /\  \     /\  \     /\__\  
  /::\  \   /:/ _/_   _\:\  \   _\:\  \    \:\  \   /::\  \   /::\  \   /:/ _/_ 
 /::\:\__\ /:/_/\__\ /::::\__\ /::::\__\   /::\__\ /::\:\__\ /::\:\__\ /::-"\__\
 \/\:\/__/ \:\/:/  / \::;;/__/ \::;;/__/  /:/\/__/ \;:::/  / \:\:\/  / \;:;-",-"
    \/__/   \::/  /   \:\__\    \:\__\    \/__/     |:\/__/   \:\/  /   |:|  |  
             \/__/     \/__/     \/__/               \|__|     \/__/     \|__|  

Share this post


Link to post
Share on other sites
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 Haggag
MHaggag''s corner

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 Haggag
MHaggag''s corner

Share this post


Link to post
Share on other sites
[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!

Share this post


Link to post
Share on other sites
I know this thread is old, but hopefully you still frequent this board Fuzztrek, and just in case anyone else needs to know the answer to this.

I had this same problem when intializing D3D you probably have a line like this somewhere:

m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)

where m_pD3D is declared as (I do it this way) CComPtr{IDirect3D9} m_pD3D (replace the brackets with less then and greater then signs). Well if you do it this way it calls addref without calling release correctly and you get those 4 memory leaks.

You can find more information at:

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dx81_c/directx_cpp/Intro/ProgramCPP/COM/UsingATLWithDx8.asp

After making the change to what they recommended I was fine. Hope it works for you or anyone else using Smart Pointers which I find as great for DX apps as you don't have to call Release.



In the making Tread Wars - Tank Battle game
Powered by SKEngine
http://www.highendgaming.com

[edited by - codeguy on February 16, 2004 10:47:45 PM]

Share this post


Link to post
Share on other sites
You need a smart pointer that lets you assign a custom deleter to call release.

check out

http://www.boost.org/libs/smart_ptr/sp_techniques.html#com


shared_ptr make_shared_from_COM(IDirect3d * p)
{
p->AddRef();
shared_ptr pw(p, mem_fn(&IDirect3d::Release));
return pw;
}


Share this post


Link to post
Share on other sites
quote:
Original post by codeguy
[...]

Just to clarify what codeguy said:
CComPtr<> increments the reference count of the source pointer on assignment. If you don''t want that you should Attach() instead of assigning.

This brings up another point: It''s always good to post a code snippet about your problem (people would''ve spotted the mistake, then)

Muhammad Haggag

Share this post


Link to post
Share on other sites