Destructor called while object is still alive

Steven Schveighoffer schveiguy at gmail.com
Sat Oct 24 11:52:27 UTC 2020


On 10/23/20 4:27 PM, frame wrote:
> On Friday, 23 October 2020 at 19:39:52 UTC, Steven Schveighoffer wrote:
>> On 10/23/20 3:23 PM, frame wrote:
>>> On Friday, 23 October 2020 at 17:47:34 UTC, Steven Schveighoffer wrote:
>>>> On 10/23/20 12:48 PM, Ola Fosheim Grøstad wrote:
>>>>> On Friday, 23 October 2020 at 16:30:52 UTC, Steven Schveighoffer 
>>>>> wrote:
>>
>>
>> 1. You generate a GC block (i.e. new class)
>> 2. You store it as part of an initializer for a stack local
>> 3. You never use it in the stack frame
>> 4. But you also in that same initialization put it into a location 
>> that is not scanned by the GC (e.g. C-malloc'd data)
>>
>> If you aren't doing all 4 of these things, then you don't have to worry.
>>
>>> Actually I do use the pointer in real code, (just one method call 
>>> before a loop but still used).
>>>
>>
>> So it shouldn't be collected then, even with optimizations.
>>
> But I doesn't do 3. and it's collected :(
> There is no special release optimization, I just call it via rdmd defaults.

Wait, so you use it inside the function after declaring it? And the 
pointer isn't stored in the stack?

This is different from your original code. Can you post an example of 
that happening?

> 
> Even if I do not run into memory problems, calling the destructor on a 
> live object is an unepxected side effect which makes the dtor concept 
> useless for me. It closes the remote connection the object hold (please 
> don't ask about the meaning, it just the way it has to work) and this 
> must not happen while the program is still running.

Sure, but if you use it during your function, it should be stored on the 
stack, and therefore not collected.

-Steve


More information about the Digitalmars-d mailing list