D-Programming-Language/dmd — pull request #2810 by yebblies (2013-11-18 06:24:17+00:00)
Title: [DDMD] Add method to RootObject to copy classes

The current method to copying ast classes does not work in D - it relies on sizeof and memcpy.

In D, sizeof(Class) gives the reference size instead of the object size, resulting in memory corruption all over the place.

Instead, do all copying with a function in RootObject, allowing the D implementation to provide a new implementation like this:

```d
final void *copyClass(size_t sz)
{
auto realsize = GC.sizeOf(cast(void*)this);
void* p = GC.malloc(realsize, GC.getAttr(cast(void*)this));
memcpy(p, cast(void*)this, realsize);
return p;
}
```

This ignores the passed size and gets the real size directly from the GC. Problem soved!