Why does destructor of global var isn't called?
Steven Schveighoffer
schveiguy at gmail.com
Wed Dec 26 14:00:08 UTC 2018
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
More information about the Digitalmars-d
mailing list