[Issue 24147] Struct destructors should not allow GC
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Sep 15 17:02:09 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=24147
elpenguino+D at gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |elpenguino+D at gmail.com
--- Comment #1 from elpenguino+D at 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- at noGC destructor from
being used with new or array reallocation instead.
--
More information about the Digitalmars-d-bugs
mailing list