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

Commit

Permalink
Merge pull request #45 from cristicbz/fix_clear
Browse files Browse the repository at this point in the history
Patch for Issue 5667 - [GSoC] "clear" does not call destructors on structs embedded in structs
  • Loading branch information
jmdavis committed Aug 12, 2011
2 parents f5f6d1d + 8cc926c commit 9e35fb4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 1 addition & 4 deletions import/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,7 @@ void clear(T)(T obj) if (is(T == class))

void clear(T)(ref T obj) if (is(T == struct))
{
static if (is(typeof(obj.__dtor())))
{
obj.__dtor();
}
typeid(T).destroy(&obj);
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
auto init = cast(ubyte[])typeid(T).init;
if(init.ptr is null) // null ptr means initialize to 0s
Expand Down
23 changes: 16 additions & 7 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -2600,10 +2600,7 @@ version(unittest) unittest

void clear(T)(ref T obj) if (is(T == struct))
{
static if (is(typeof(obj.__dtor())))
{
obj.__dtor();
}
typeid(T).destroy( &obj );
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
auto init = cast(ubyte[])typeid(T).init();
if(init.ptr is null) // null ptr means initialize to 0s
Expand All @@ -2622,20 +2619,32 @@ version(unittest) unittest
assert(a.s == "A");
}
{
static bool destroyed = false;
static int destroyed = 0;
struct C
{
string s = "C";
~this()
{
destroyed ++;
}
}

struct B
{
C c;
string s = "B";
~this()
{
destroyed = true;
destroyed ++;
}
}
B a;
a.s = "asd";
a.c.s = "jkl";
clear(a);
assert(destroyed);
assert(destroyed == 2);
assert(a.s == "B");
assert(a.c.s == "C" );
}
}

Expand Down

0 comments on commit 9e35fb4

Please sign in to comment.