Rainer Deyke wrote:
Robert Jacques wrote:
RAII is all about stack allocation over heap allocation (or so I
thought). Ah, wikipedia has set me straight. Anyways, now for the simple
answer: you can't create D1 arrays with RAII types, I think. (Anyone
tried scope Foo[] foo?) Anyways, in D2, if I remember correctly there's
a bug where struct finilizers don't run if they're allocated on the
heap. But if you're using classes for RAII like you should, the GC will
run their finalizers just fine after the array dies. But this is an
seems to be an issue about the elements/values inside the containers,
not the container itself. So I'm lost.

A RAII variable is "destroyed" when it goes out of scope, where
"destroyed" means that a destructor is called.  RAII is a transitive
feature.  When a RAII variable is destroyed, its members are also
destroyed.  When a RAII container is destroyed, all of its contents are
destroyed.

References in D are not RAII types, because when a reference goes out of
scope, the "contents" of that reference are not destroyed until the
garbage decides to collect them, at which point it is too late to
perform clean-up.

When an array dies, its contents are destroyed.  The issue is when the
array dies.

If the array is a value type, the array dies when it goes out of scope,
so RAII is possible.

If the array is a reference type, the array dies when the garbage
collector decides to run sometime after all live references to the array
have died, so RAII is not possible.

RAII can be implemented even with reference semantics. The mechanics would involve reference counting.

Andrei

Reply via email to