~= call copy ctor?

monarch_dodra monarchdodra at gmail.com
Thu Jul 19 09:08:04 PDT 2012


On Thursday, 19 July 2012 at 15:36:01 UTC, Namespace wrote:
> _counter is still 1 but the scope is released. How is that 
> possible?
> Even with _arr.clear(); at the end of the scope, _counter is 
> still 1.
> I see one CTor and one Copy CTor but only one DTor.

_arr is actually a dynamic array, which allocates from the 
garbage collector. When _arr goes out of scope, you are only 
destroying the "Slice" that is looking at the data, but not the 
data itself. You have to wait until the Garbage Collector runs 
and collects.

Either that, or use a stack allocated static array, but I 
wouldn't recommend it.

Furthermore, D does not actually guarantee that all your objects 
are destroyed (eg. destructors called) when the program ends. D's 
approach is "The program has ended, ergo anything left in memory 
is moot. Just let the OS clear it."

If you really need RAII, you can use RefCounted objects, such as 
RefCounted! directly, or by using Array.


More information about the Digitalmars-d-learn mailing list