Fix #23458 - Add pragma for C++ deleting destructors - #23459
Conversation
Currently deleting destructors for C++ are implemented in D by only running the normal destructors and not freeing memory. This can result in memory leaks if instances are deleted from C++. Implementing deleting destructors like in C++ would break existing D code, because it requires linking the C++ standard library. Instead this PR adds a pragma for enabling freeing memory in deleting destructors for classes. The pragma is inherited by subclasses, so it normally only needs to be used on root classes. For classes, which are not `extern(C++)`, or other declarations the pragma is ignored. In a future edition the pragma could be enabled by default.
| int cppDtorVtblIndex = -1; | ||
|
|
||
| /// free memory in deleting destructor | ||
| bool cppUseDelDtor; |
There was a problem hiding this comment.
Hmm these really should be turned into a bitfield if this is merged.
There was a problem hiding this comment.
Yes, that would be good for the future. The added bool fields don't change the class size here, because the alignment leaves space, but other fields could also be combined into a bitfield.
DMD perf check
|
| pragma(cpp_use_deleting_destructor, true) | ||
| class DBase |
There was a problem hiding this comment.
test also struct? Or os the deleting destructor an inheritance thing?
There was a problem hiding this comment.
The deleting destructor is only needed for classes, because it is placed in the vtbl. The pragma is ignored for structs and other symbols.
Adding tests for struct could still make sense, because I don't think there are existing tests checking if the C++ memory is really freed.
Currently deleting destructors for C++ are implemented in D by only running the normal destructors and not freeing memory. This can result in memory leaks if instances are deleted from C++.
Implementing deleting destructors like in C++ would break existing D code, because it requires linking the C++ standard library. Instead this PR adds a pragma for enabling freeing memory in deleting destructors for classes. The pragma is inherited by subclasses, so it normally only needs to be used on root classes.
For classes, which are not
extern(C++), or other declarations the pragma is ignored.In a future edition the pragma could be enabled by default.