Destructor called while object is still alive

Daniel Kozak kozzi11 at gmail.com
Fri Oct 23 06:37:43 UTC 2020


On Fri, Oct 23, 2020 at 8:20 AM frame via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

>  Even this is a "intelligent"
> feature by the compiler for non reachable code like that loop,
> it's still confusing. There are maybe situations while the object
> should stay in background, eg. socket related/event stuff. If
> there is a valid pointer, there is no excuse to reap it.
>
>
There is no use of foo anywhere so there is no reason to not collect it.
You even does not need to have while in your code:

void main() {
     auto foo = Bar.create();
     writefln("address: %s", foo.__vptr);
     GC.collect();
     Thread.sleep(1.seconds);
     //foo.sayHello(); // if uncommented GC would not call destructor until
end of main
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20201023/c5d4e617/attachment.htm>


More information about the Digitalmars-d mailing list