RAII and Deterministic Destruction

Jim Hewes via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 26 09:36:00 PDT 2015


Thanks for all the info. It's a good comparison of structs and classes 
to keep handy. Actually, I'm fine with the GC. I don't mean to avoid it. 
I just would like some way to also have non-memory resources 
automatically released in a timely, predictable way.

One common thing to do in C++ is to manage the lifetime of an object 
with std::unique_ptr but then use its .get() function to get a native 
pointer to use temporarily. You just have to ensure that the native 
pointer doesn't outlive the unique_ptr, which isn't that difficult. 
True, if the unique_ptr gets destructed the native pointer is invalid 
and that could be risky, but you limit its use.
Unique in D doesn't seem to have anything like a get() function but I 
wonder if it could. That is, it would get another "native" reference to 
the Unique resource that is managed by the garbage collector. So if the 
Unique went out of scope and got destructed, the reference would at 
least refer to valid memory although not a valid object because its 
destructor had already been called. Not perfectly safe, but no worse 
than the C++ case. Just a thought.

Jim


More information about the Digitalmars-d-learn mailing list