December 22, 2023
https://issues.dlang.org/show_bug.cgi?id=24298

          Issue ID: 24298
           Summary: cpp_delete should check for null
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: tim.dlang@t-online.de

Function core.stdcpp.new_.cpp_delete is the equivalent of the delete operator in C++, which can be called with a null pointer and ignores it. Calling cpp_delete with a null pointer can currently result in a crash, which makes porting of C++ code to D harder.

unittest
{
    import core.stdcpp.new_: cpp_new, cpp_delete;

    extern(C++) static struct S
    {
        __gshared int numDeleted;
        __gshared int lastDeleted;
        int i;
        ~this()
        {
            lastDeleted = i;
            numDeleted++;
        }
    }
    S *s = cpp_new!S(12345);
    cpp_delete(s);
    assert(S.numDeleted == 1);
    assert(S.lastDeleted == 12345);
    s = null;
    cpp_delete(s);
    assert(S.numDeleted == 1);
    assert(S.lastDeleted == 12345);
}

--