September 15, 2023 [Issue 24147] Struct destructors should not allow GC | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=24147 elpenguino+D@gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |elpenguino+D@gmail.com --- Comment #1 from elpenguino+D@gmail.com --- I think this is the wrong way to approach the issue. It is perfectly valid for a destructor to allocate using the GC, as long as the struct itself is not allocated using the GC. Example code: ``` import std.stdio; struct Predictable { string id; this(string _id) { writeln("Constructor ", _id); id = _id; } ~this() { writeln("Destructor " ~ id); } } void main() { Predictable result = Predictable("1"); } ``` outputs two lines, 'Constructor 1' and 'Destructor 1' without issue. I'd say it's better to simply forbid anything with a non-@noGC destructor from being used with new or array reallocation instead. -- |
Copyright © 1999-2021 by the D Language Foundation