Don't expect class destructors to be called at all by the GC

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Dec 21 18:48:38 UTC 2017


On Thu, Dec 21, 2017 at 06:45:27PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote:
> On Thursday, 21 December 2017 at 18:20:19 UTC, H. S. Teoh wrote:
> > When the scoped destruction of structs isn't an option, RefCounted!T
> > seems to be a less evil alternative than an unreliable class dtor.
> > :-/
> 
> Alas, RefCounted doesn't work well with inheritance...

Oh?  What's the issue?


> Though, what you could do is make the refcounted owners and borrow the
> actual reference later.

Yeah, I figured that I pretty much have to use a proxy struct with
RefCounted, and have the struct dtor do the actual cleanup of the class
reference, something like:

	class Resource {
		void cleanup(); // inheritable
	}
	struct Proxy {
		private Resource res;
		this(Resource _res) { res = _res; }
		~this() { res.cleanup(); }
	}
	...
	auto obj = RefCounted!Proxy(allocateResource());


T

-- 
The problem with the world is that everybody else is stupid.


More information about the Digitalmars-d-learn mailing list