Destructor called while object is still alive

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Fri Oct 23 16:48:30 UTC 2020


On Friday, 23 October 2020 at 16:30:52 UTC, Steven Schveighoffer 
wrote:
> No, D classes map to C++ pointers to classes.

Huh? D has to be able to call virtual destructors for C++ objects.

> How does C++ RAII work for class pointers that aren't used?

What do you mean? The pointer is valid until the end of the 
scope. Then the object is destructed if it is a sole owning 
pointer.

_all_ GC pointers are conceptually owning pointers. They have to 
stay live throughout the whole scope.


> Expectation for what purpose?

Basic CS conventions?


> I think of code being executed in the way I write it. We all 
> know that optimizers don't do that. As long as it's not 
> changing the semantic meaning of the program, I'm fine with it.

But it did! That was the point!


>
> If you look at this code:
>
> void main()
> {
>    int i;
>    foo();
> }
>
> Does i even exist? Does it exist after the first line? Is it 
> important if the compiler allocates space for it on the stack? 
> Does it get initialized to 0?

What do you mean? There are no side effects? Do D allow side 
effects in destructors? If yes, then you cannot cap the lifetime 
of the object at your own will.

> just completely get rid of the variable storage. This is no 
> different.

It clearly is. The order of the writeln() execution was wrong.

> I think the optimizer is correct. There are no live pointers, 
> because the object is not used, ever.

Of course it is used. It has a destructor with a side effect.




More information about the Digitalmars-d mailing list