Class destructors - clarify what is safe

Forum User forumuser at example.com
Sun Feb 15 09:30:12 UTC 2026


On Sunday, 15 February 2026 at 01:36:55 UTC, H. S. Teoh wrote:
> Basically, the GC finds the set of dead objects in an 
> unspecified order, which from the POV of user code should be 
> regarded as random and unpredictable.  Consider an object c of 
> class C:
>
> ```
> 	class C {
> 		A a;
> 		B b;
> 		this(A _a, B _b) { a = _a; b = _b; }
> 		~this() {
> 		}
> 	}
>
> 	auto a = new A;
> 	auto b = new B;
> 	auto c = new C(a, b);
> ```
>
> When c goes out of scope, a and b are still referenced by c, 
> but are no longer reachable from the rest of the program.  The 
> order in which the GC finds dead objects is not specified

Is it possible that during stack unwinding the GC kicks in and 
destroys an exception object before it is caught?


More information about the Digitalmars-d-learn mailing list