www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22505] New: Array relocation causes multiple calls to

https://issues.dlang.org/show_bug.cgi?id=22505

          Issue ID: 22505
           Summary: Array relocation causes multiple calls to destructor
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: omerfirmak gmail.com

import std;
import core.memory;

struct B
{
        int[] b;
    ~this ()
    {
                b[0]++;
                writeln(b[0]);
    }
}

void main()
{
        B[] b_list;
        foreach(idx; 0 ..100)
            b_list ~= B([0]);
        b_list.length = 0;
    GC.collect();
}

---------------------------------------------------------

One would assume that B.b[0] would always be 0 before destructor runs. But if
b_list is ever relocated while trying to expand its size, GC will finalize
objects while trying to collect the old memory for b_list and cause all objects
in b_list to be finalized multiple times.

--
Nov 12 2021