Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 25, 2018 Why does destructor of global var isn't called? | ||||
---|---|---|---|---|
| ||||
example: ----------- struct A{ int* arr; ~this() { writeln("A destruct"); } } static ~this() { writeln("module destruct"); } A a; void main() { } ----------- only prints "module destruct" |
December 25, 2018 Re: Why does destructor of global var isn't called? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dru | 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.
|
December 26, 2018 Re: Why does destructor of global var isn't called? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basilez B. | 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
|
December 26, 2018 Re: Why does destructor of global var isn't called? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | yeah it was reported https://issues.dlang.org/show_bug.cgi?id=14650 Status: ASSIGNED Importance: P1 normal Assignee: Seb |
December 26, 2018 Re: Why does destructor of global var isn't called? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dru | 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.
|
Copyright © 1999-2021 by the D Language Foundation