Destruction in D

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 1 10:45:00 PDT 2015


On Friday, 1 May 2015 at 02:35:52 UTC, Idan Arye wrote:
> On Thursday, 30 April 2015 at 23:27:49 UTC, bitwise wrote:
>> Well, the third thing was just my reasoning for asking in the 
>> first place. I need to be able to acquire/release shared 
>> resources reliably, like an OpenGL texture, for example.
>
> If you want to release resources, you are going to have to call 
> some functions that do that for you, so you can't escape that 
> "special stack frame"(what's so special about it?) - though the 
> compiler might inline it.
>
> When you use a GC the compiler don't need to invoke the 
> destructor in the end of the scope because the object is 
> destroyed in the background, but that also means you can't rely 
> on it to release your resources, so languages like Java and C# 
> use try-with-resources and using statements(corresponding) to 
> call something at the end of the scope and end up using that 
> stack frame anyways.

I'm not sure I understand you 100%, but my plan was to have an 
asset management system give out ref counted textures/etc. 
Whenever the last one went out of scope, the asset would be 
destroyed. This only works if the destructor is called on the 
graphics thread due to limitations of graphics APIs. In a single 
threaded C++ app, this is fine, destructors are called at end of 
scope. I was confused though, because like C#, D has both 
reference and value types. But, while in C#, value types still do 
not have destructors(grrr...) D structs do have destructors, 
which apparently run when the struct goes out of scope. However, 
the D port of my code will most likely use multithreaded 
rendering, which removes the guarantee that the assets will go 
out of scope on the graphics thread, so this idea is a no-go 
anyways.


More information about the Digitalmars-d-learn mailing list