What is the purpose of -preview=dtorfields?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Aug 31 20:34:23 UTC 2020


On Mon, Aug 31, 2020 at 08:22:02PM +0000, Per Nordlöw via Digitalmars-d wrote:
> I'd appreciate if somebody elaborated a bit on the purpose of the
> compiler flag
> 
>     -preview=dtorfields       destruct fields of partially constructed
> objects

My guess is to be completely exception-safe. For example:

	class Resource(T) {
		T* handle;
		this(...) { handle = acquireResource(...); }
		~this() { releaseResource(handle); }
	}

	class ResCollection {
		Resource!A resA;
		Resource!B resB;

		this() {
			resA = new Resource!A(...);
			resB = new Resource!B(...);
		}
	}

If an exception is thrown during the call to `new Resource!B`, the
current behaviour is that resA is not destructed, so there may
potentially be a resource leak.  The abovementioned compiler flag is to
force destruction of resA in case resB fails to be initialized (i.e., as
if you wrote `scope(failure) delete(resA);` after the `resA = ...`
line).


T

-- 
Кто везде - тот нигде.


More information about the Digitalmars-d mailing list