sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 641 forks source link

Current status of lifetime management? #967

Open ziriax opened 6 years ago

ziriax commented 6 years ago

In the early days of SharpDX, it was adviced to explicitly call Dispose, not allowing the garbage collector to perform cleanup in the finalizers. Since the .NET runtime doesn't have a concept of GPU memory pressure, I assume this is still the case, but in the past, this often just crashed. This is even stated in the DirectX FAQ:

Does it matter in which order I release DirectX interfaces? It shouldn't matter because COM interfaces are reference counted. However, there are some known bugs with the release order of interfaces in some versions of DirectX. For safety, you are advised to release interfaces in reverse creation order when possible.

What is current status on this? Since we are building a playground for experimenting, we don't care that much about device resources immediately being released, and we would like to just use the garbage collector.

Thanks a lot, Peter Verswyvelen

xoofx commented 6 years ago

What is current status on this?

The advice still prevails. Calling Dispose on SharpDX object should always work. And indeed, the feature of auto-disposing has been made optional in SharpDX (you can still activate it via Configuration), but as stated I don't guarantee that it will always work.

Also, as you said, the Dispose pattern is mostly fine for single ownership, so if you can stick with this pattern it is better. If you can't, you have either to track things with AddRef/ReleaseRef (which is cumbersome) or let auto-dispose working.

ziriax commented 6 years ago

Thanks! We really need destructible value types in C# so we can have smart pointers :-(