Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Resources with smart pointers?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Deortuka   Members   -  Reputation: 435

Like
0Likes
Like

Posted 18 March 2013 - 02:40 PM

I recently had an idea to wrap my DX11 resource pointers (ID3D11Buffer*, ID3D11RasterizerState*, etc...) so that Release would automatically be called once they are deleted from the vector that I use to save all created resources.

 

I then had the idea to check out STL to see if shared_ptr would provide the necessary functionality. So far it seems all I have to do is add a custom deleter function that calls Release().

 

I wanted to know if anyone has tried this approach before. If so, does it work as expected?

 

Thanks in advance.



Sponsor:

#2 chetanjags   Members   -  Reputation: 166

Like
3Likes
Like

Posted 18 March 2013 - 02:48 PM

Hi, I think maybe you are looking for ComPtr already provided by microsoft http://msdn.microsoft.com/en-us/library/br244983.aspx


My Game Development Blog : chetanjags.wordpress.com


#3 turch   Members   -  Reputation: 569

Like
1Likes
Like

Posted 18 March 2013 - 03:20 PM

As Chetanhl said, use ComPtr

 

http://legalizeadulthood.wordpress.com/2009/01/11/direct3d-programming-tip-3-use-smart-pointers/



#4 Deortuka   Members   -  Reputation: 435

Like
0Likes
Like

Posted 19 March 2013 - 09:48 PM

Thanks a lot for the replies. Unfortunately, I am still left curious as to why the use of CComPtr in Direct3D programming is not recommended in the D3D documentation nor samples.



#5 ray_intellect   Crossbones+   -  Reputation: 220

Like
0Likes
Like

Posted 20 March 2013 - 02:50 PM

Perhaps you could use Java instead and automatically release all your textures without any extra code 



#6 Jason Z   GDNet+   -  Reputation: 2372

Like
0Likes
Like

Posted 20 March 2013 - 03:12 PM

Perhaps you could use Java instead and automatically release all your textures without any extra code 

Ummm - I don't think you can use Direct3D with Java and just 'automatically release all your textures without any extra code'.  Regardless of what programming language you use, Direct3D is a COM based interface.  You have to use the COM methodology regardless of the reference counting scheme - either garbage collection in Java or smart pointers/ComPtr in C++.


Jason Zink :: DirectX MVP
Check out our (now available) D3D11 book: Practical Rendering and Computation with Direct3D 11
Check out my Direct3D 11 engine on CodePlex: Hieroglyph 3
Check out our free online D3D10 book: Programming Vertex, Geometry, and Pixel Shaders
Lunar Rift :: Dual-Paraboloid Mapping Article :: Parallax Occlusion Mapping Article :: Fast Silhouettes Article

#7 phil_t   Members   -  Reputation: 1062

Like
0Likes
Like

Posted 20 March 2013 - 03:52 PM

Perhaps you could use Java instead and automatically release all your textures without any extra code

Automatic memory management doesn't eliminate the need to manage native resources like graphic devices, textures, etc..

When you release a texture, you usually want it to be gone from memory immediately, not "some time later". So you end up having to write extra code (e.g. disposable pattern) in managed environments too.

Edited by phil_t, 20 March 2013 - 03:58 PM.


#8 SiCrane   Moderators   -  Reputation: 6658

Like
0Likes
Like

Posted 20 March 2013 - 04:38 PM

Unfortunately, I am still left curious as to why the use of CComPtr in Direct3D programming is not recommended in the D3D documentation nor samples.

For the most part the DirectX samples assume that you are already familiar with COM. Things like smart pointers are mentioned in Microsoft's COM coding guides.

And yes, people have tried using shared_ptr with COM interfaces. However, it does work slightly weirdly. Since shared_ptr uses an external reference count in addition to the COM internal reference count, weak_ptr doesn't know if there are still non-shared_ptr references keeping an object alive that the shared_ptr group doesn't know about, so the object will look deleted to a weak_ptr when it's actually still alive.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS