www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does destructor of global var isn't called?

reply Dru <Dru notreal.com> writes:
example:
-----------

struct A{
	int* arr;
	
	~this() {
		writeln("A destruct");
	}
}

static ~this() {
	writeln("module destruct");
}

A a;

void main() {
}

-----------

only prints "module destruct"
Dec 25 2018
parent reply Basilez B. <b2.temp gmx.com> writes:
On Tuesday, 25 December 2018 at 19:32:47 UTC, Dru wrote:
 example:
 -----------

 struct A{
 	int* arr;
 	
 	~this() {
 		writeln("A destruct");
 	}
 }

 static ~this() {
 	writeln("module destruct");
 }

 A a;

 void main() {
 }

 -----------

 only prints "module destruct"
`a`, since declared in the global scope, is constructed at compile-time so its data are in executable dedicated segments (althoufh here it's just equal to A.init) and don't have to be destructed. Add `a = A(null); ` in main and the destructor gets called.
Dec 25 2018
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/25/18 3:46 PM, Basilez B. wrote:
 On Tuesday, 25 December 2018 at 19:32:47 UTC, Dru wrote:
 example:
 -----------

 struct A{
     int* arr;

     ~this() {
         writeln("A destruct");
     }
 }

 static ~this() {
     writeln("module destruct");
 }

 A a;

 void main() {
 }

 -----------

 only prints "module destruct"
`a`, since declared in the global scope, is constructed at compile-time so its data are in executable dedicated segments (althoufh here it's just equal to A.init) and don't have to be destructed. Add `a = A(null); ` in main and the destructor gets called.
That's the destructor of the temporary, not the `a`. Add a writeln("end of main") at the end of main and you will see the destructor is called before main exits (while `a` is still alive). My expectation is that thread-local-storage variables are not destroyed. It's definitely an oversight, as things like refcounting would be screwed up if a reference is stored there. However, I think this will take some help from the compiler, and possibly we would have to put those destructors in a constructed static destructor, since there is no "type" that holds those variables. This seems like something for which a bug likely already exists, but feel free to file one if you don't find it. -Steve
Dec 26 2018
parent reply Dru <dru notreal.com> writes:
yeah it was reported
https://issues.dlang.org/show_bug.cgi?id=14650

Status: 	ASSIGNED
Importance: 	P1 normal
Assignee: 	Seb
Dec 26 2018
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 12/26/18 1:45 PM, Dru wrote:
 yeah it was reported
 https://issues.dlang.org/show_bug.cgi?id=14650
 
 Status:     ASSIGNED
 Importance:     P1 normal
 Assignee:     Seb
Reassigned to Razvan.
Dec 26 2018