Skip to content

Commit a63ae87

Browse files
kinkedlang-bot
authored andcommitted
Fix Issue 23129 - Don't re-initialize D class instances in destroy!false()
When it's explicitly *not* requested.
1 parent 94bd5bc commit a63ae87

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/object.d

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,7 +4428,7 @@ nothrow @safe @nogc unittest
44284428
}
44294429
}
44304430

4431-
private extern (C) void rt_finalize(void *data, bool det=true) nothrow;
4431+
private extern (C) void rt_finalize2(void* p, bool det = true, bool resetMemory = true) nothrow;
44324432

44334433
/// ditto
44344434
void destroy(bool initialize = true, T)(T obj) if (is(T == class))
@@ -4448,7 +4448,7 @@ void destroy(bool initialize = true, T)(T obj) if (is(T == class))
44484448
{
44494449
// Bypass overloaded opCast
44504450
auto ptr = (() @trusted => *cast(void**) &obj)();
4451-
rt_finalize(ptr);
4451+
rt_finalize2(ptr, true, initialize);
44524452
}
44534453
}
44544454

@@ -4723,6 +4723,25 @@ nothrow unittest
47234723
destroy(B.init);
47244724
}
47254725

4726+
// make sure destroy!false skips re-initialization
4727+
unittest
4728+
{
4729+
static struct S { int x; }
4730+
static class C { int x; }
4731+
static extern(C++) class Cpp { int x; }
4732+
4733+
static void test(T)(T inst)
4734+
{
4735+
inst.x = 123;
4736+
destroy!false(inst);
4737+
assert(inst.x == 123, T.stringof);
4738+
}
4739+
4740+
test(S());
4741+
test(new C());
4742+
test(new Cpp());
4743+
}
4744+
47264745
/// ditto
47274746
void destroy(bool initialize = true, T)(ref T obj)
47284747
if (__traits(isStaticArray, T))

0 commit comments

Comments
 (0)