Destructor called while object is still alive
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Fri Oct 23 08:32:45 UTC 2020
On Friday, 23 October 2020 at 07:39:37 UTC, Daniel N wrote:
> On Friday, 23 October 2020 at 07:32:02 UTC, Ola Fosheim Grøstad
> wrote:
>> On Friday, 23 October 2020 at 06:15:32 UTC, frame wrote:
>>> Not sure if this is expected behaviour, but I find this weird:
>>> The destructor is called on the object just because it gets
>>> out of scope?
>>
>> The destructor may not be called at all by the GC, or it might
>> be called whenever there is no pointer found in
>> memory/registers. Since D is essentially using C-style
>> code-gen/optimizers that has little to no knowledge of a GC it
>> may be called prematurely or never.
>>
>
> Simple fix:
> auto foo = Bar.create();
> =>
> scope foo = Bar.create();
Does not work with LDC -O3:
void main() {
scope foo = Bar.create();
GC.collect();
writefln("Collection done");
exit(0);
}
Output:
Warning: destructor called (onlineapp.Foo) on 55A899C1C570
Collection done
More information about the Digitalmars-d
mailing list