Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 510d95a

Browse files
authored
Merge pull request #2189 from TurkeyMan/cpp_aggregate_dtor
Important to clean up aggregates too! merged-on-behalf-of: David Nadlinger <code@klickverbot.at>
2 parents 38d784a + 607873c commit 510d95a

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

src/object.d

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ void destroy(T)(T obj) if (is(T == class))
29302930
{
29312931
static if(__traits(getLinkage, T) == "C++")
29322932
{
2933-
obj.__dtor();
2933+
obj.__xdtor();
29342934

29352935
enum classSize = __traits(classInstanceSize, T);
29362936
(cast(void*)obj)[0 .. classSize] = typeid(T).initializer[];
@@ -2950,38 +2950,66 @@ unittest
29502950
{
29512951
class C
29522952
{
2953+
struct Agg
2954+
{
2955+
static int dtorCount;
2956+
2957+
int x = 10;
2958+
~this() { dtorCount++; }
2959+
}
2960+
29532961
static int dtorCount;
29542962

29552963
string s = "S";
2964+
Agg a;
29562965
~this() { dtorCount++; }
29572966
}
29582967

29592968
C c = new C();
2960-
assert(c.dtorCount == 0); // destructor not yet called
2961-
assert(c.s == "S"); // initial state `c.s` is `"S"`
2969+
assert(c.dtorCount == 0); // destructor not yet called
2970+
assert(c.s == "S"); // initial state `c.s` is `"S"`
2971+
assert(c.a.dtorCount == 0); // destructor not yet called
2972+
assert(c.a.x == 10); // initial state `c.a.x` is `10`
29622973
c.s = "T";
2963-
assert(c.s == "T"); // `c.s` is `"T"`
2974+
c.a.x = 30;
2975+
assert(c.s == "T"); // `c.s` is `"T"`
29642976
destroy(c);
2965-
assert(c.dtorCount == 1); // `c`'s destructor was called
2966-
assert(c.s == "S"); // `c.s` is back to its inital state, `"S"`
2977+
assert(c.dtorCount == 1); // `c`'s destructor was called
2978+
assert(c.s == "S"); // `c.s` is back to its inital state, `"S"`
2979+
assert(c.a.dtorCount == 1); // `c.a`'s destructor was called
2980+
assert(c.a.x == 10); // `c.a.x` is back to its inital state, `10`
29672981

29682982
// check C++ classes work too!
29692983
extern (C++) class CPP
29702984
{
2985+
struct Agg
2986+
{
2987+
__gshared int dtorCount;
2988+
2989+
int x = 10;
2990+
~this() { dtorCount++; }
2991+
}
2992+
29712993
__gshared int dtorCount;
29722994

29732995
string s = "S";
2996+
Agg a;
29742997
~this() { dtorCount++; }
29752998
}
29762999

29773000
CPP cpp = new CPP();
2978-
assert(cpp.dtorCount == 0); // destructor not yet called
2979-
assert(cpp.s == "S"); // initial state `c.s` is `"S"`
3001+
assert(cpp.dtorCount == 0); // destructor not yet called
3002+
assert(cpp.s == "S"); // initial state `cpp.s` is `"S"`
3003+
assert(cpp.a.dtorCount == 0); // destructor not yet called
3004+
assert(cpp.a.x == 10); // initial state `cpp.a.x` is `10`
29803005
cpp.s = "T";
2981-
assert(cpp.s == "T"); // `c.s` is `"T"`
3006+
cpp.a.x = 30;
3007+
assert(cpp.s == "T"); // `cpp.s` is `"T"`
29823008
destroy(cpp);
2983-
assert(cpp.dtorCount == 1); // `c`'s destructor was called
2984-
assert(cpp.s == "S"); // `c.s` is back to its inital state, `"S"`
3009+
assert(cpp.dtorCount == 1); // `cpp`'s destructor was called
3010+
assert(cpp.s == "S"); // `cpp.s` is back to its inital state, `"S"`
3011+
assert(cpp.a.dtorCount == 1); // `cpp.a`'s destructor was called
3012+
assert(cpp.a.x == 10); // `cpp.a.x` is back to its inital state, `10`
29853013
}
29863014

29873015
/// Value type demonstration

0 commit comments

Comments
 (0)