Threading bugs

Sean Kelly sean at f4.ca
Wed Jun 27 12:47:38 PDT 2007


Tristam MacDonald wrote:
> I guess coming from a C++ background (i.e. no GC), I am having trouble with the whole idea of destructors-as-finalizers, meaning only used to free memory, rather than to manage resources.

They can do both, but if it's important that those resources are 
explicitly released then you should take steps to manage the object's 
lifetime.  I could still be convinced that all lingering objects should 
be collected on shutdown, but I haven't come up with an approach I'm 
entirely happy with (the most obvious being to simply collect everything 
and if an exception is thrown then give up, but continue the rest of the 
shutdown process).

> To me this seems a big hole in the language. I know 'scope' is supposed to be used for RAII, but it practice it falls short, due to the inability to return or copy scoped classes in a useful manner. This leads to lots of C-style explicit reference counting (obj.retain(), obj.release(), etc.), without even the C++ convinience of wrapping it in 'fake' pointers.

Yup.  It doesn't help that there is no way to implement smart pointers 
in D, given the lack of copy semantics in structs.

> AFAIK, no GC'd language has come up with a good solution, and there obviously isn't a simple offhand fix. Maybe constructors/destructors/copying of structs would fill this hole by allowing high-level value types, which would solve RAII by dint of residing on the stack. 

FWIW, Tango has a means of hooking the collection routine, which may be 
used as a means of detecting resource 'leaks'.


Sean


More information about the Digitalmars-d-learn mailing list