free causes exception

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 27 15:04:25 PST 2016


On 1/27/16 5:35 PM, Igor wrote:
> On Wednesday, 27 January 2016 at 14:31:20 UTC, Steven Schveighoffer wrote:

>> All D destructors should destroy all the members. And generally
>> speaking, if you ever plan to use a class with the GC, you should only
>> destroy non-GC members. The GC members may already be destroyed.
>>

BTW, I wrote that wrong, I should have said "All D destructors *should 
do is* destroy all the members." In other words, you shouldn't do 
anything but destroy members.

>
> There needs to be better docs on this? Or at least someone needs to
> provide a link! ;)


http://dlang.org/spec/class.html#destructors

"Furthermore, the order in which the garbage collector calls destructors 
for unreference objects is not specified. This means that when the 
garbage collector calls a destructor for an object of a class that has 
members that are references to garbage collected objects, those 
references may no longer be valid. **This means that destructors cannot 
reference sub objects.**"

(emphasis mine)

> Why can there be an "deplace" equivalent to emplace?

emplace is not responsible for allocation, it just does all the 
necessary things to place an object inside memory you provide (wherever 
it came from), and call the proper constructor.

The opposite of emplace is destroy. This calls the destructor and resets 
the class to a "destroyed" state (so calling destroy on a class a second 
time will not call the destructor again).

> Everone says it's so easy not to use the GC in D yet I can't seem to
> find any real world examples ;/

The easiness of doing it is a judgment call. However, it is possible.

I'm sure there are examples out there, but I'm not well versed in where 
to find them.

-Steve


More information about the Digitalmars-d-learn mailing list