Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to delete in D?

I'm learning D from 8 years in C++. My question is with regards to D garbage collection - when do I use delete, and when don't I need to?

like image 829
bfops Avatar asked Jan 03 '11 22:01

bfops


People also ask

Is it OK to delete D drive?

The D: drive is usually a secondary hard drive installed on a computer, often used to hold the restore partition or to provide additional disk storage space. You may decide to clean off the contents of the D: drive to free up some space or perhaps because the computer is being assigned to another worker in your office.

Can I delete all the files in my D drive?

Right-click partition D and choose "Delete Volume". 2. Click "Yes" to execute, and all data and files on the D drive will be deleted.


1 Answers

You don't. Delete is not to be used with D version 2 and intended to be removed from the language. What the hold up is, I am not sure. Instead you use a function, destroy(object), which calls the destructor where you can free resources that are not GC memory. The destructor will be caused again during GC collection of the objects own memory. This is explained in "The D Programming Language".

The idea is to reclaim resources earlier than what the GC would provide and prevents memory corruption from dangling pointers. To be less safe the core.memory module provides GC.free(object) which can be used to free the memory, after calling destroy(object).

As I'm not a C++ programmer, I don't really know the RAII pattern, but this and reference counting is the expected strategy if you wish to avoid the GC.

like image 117
he_the_great Avatar answered Jan 04 '23 08:01

he_the_great