Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing resources in D language

Tags:

People also ask

What is the D programming language used for?

D has been successfully used for AAA games, language interpreters, virtual machines, an operating system kernel, GPU programming, web development, numerical analysis, GUI applications, a passenger information system, machine learning, text processing, web and application servers and research.

Does D have garbage collection?

D is a systems programming language with support for garbage collection. Usually it is not necessary to free memory explicitly. Just allocate as needed, and the garbage collector will periodically return all unused memory to the pool of available memory.

What is garbage programming language?

In computer science, garbage includes data, objects, or other regions of the memory of a computer system (or other system resources), which will not be used in any future computation by the system, or by a program running on it.

Is rust better than C++?

You can easily notice similarities between Rust and C++ syntax, but Rust offers a higher level of memory safety without using a garbage collector. Not for the first time, Rust has been named the most loved language—it gained more than 86% of developers' votes.


When using Direct3D in c++ I can write a "Cube" class for example, that contains a "ID3D11Buffer* vertexBuffer_" and ensure that the destructor for that Cube object calls vertexBuffer_->Release().

I can have a "Scene" class containing a "unique_ptr cube_" object. So that I know that when I delete my scene, the cube will be deleted, and that will consequently call release on the D3D resources it is using.

In D I can't do this. I can write destructors but I have no idea when they will be called. If the GC doesn't require the memory they may never be called...

So what is the best way to handle this kind of thing in D? I could add a "Free" member function to each of the objects that frees all of it's own resources and calls "Free" on any objects it owns, but this seems an error prone manual operation and a step backwards from C++.

Is there a better way to handle this kind of thing in D?