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);
}
Comment #1 by dlang-bot — 2023-12-22T13:13:55Z
@tim-dlang created dlang/dmd pull request #15943 "Fix issue 24298 - cpp_delete should check for null" fixing this issue:
- Fix issue 24298 - cpp_delete should check for null
The delete operator in C++ can be called with a null pointer and
ignores it. This commit makes cpp_delete consistent with C++, so null
pointers are also ignored.
https://github.com/dlang/dmd/pull/15943
Comment #2 by dlang-bot — 2023-12-22T21:45:15Z
dlang/dmd pull request #15943 "Fix issue 24298 - cpp_delete should check for null" was merged into master:
- a70e1771f8f9fa56e111859feaa8800d17755dd7 by Tim Schendekehl:
Fix issue 24298 - cpp_delete should check for null
The delete operator in C++ can be called with a null pointer and
ignores it. This commit makes cpp_delete consistent with C++, so null
pointers are also ignored.
https://github.com/dlang/dmd/pull/15943