D2: bug in struct postblit/dtor, or am I a burke?

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 17 23:32:56 PDT 2009

Hi all.

I can't seem to get this to work properly.  I'm trying to write a
copy-on-write proxy type.  I'm using postblit and dtors along with the
usual ctors and opAssign to manage the refcount of the shared memory.
However, testing with dmd-2.026 (and a few previous versions a little
while ago), the attached program gives me the following output:

(NB: ">" means entering, "<" means leaving; these messages are printed
from in- and out-contracts.  Printed values are the array being stored
and the ref count (or zero if the ref count pointer is null.))

> opAssign(T[]);            COWArray(0x    0000[0..0]) @ 0
> ~this();                  COWArray(0x  12FE88[0..4424652]) @ 1245120
< ~this();                  COWArray(0x  12FE88[0..4424652]) @ 1245119
< opAssign(T[]);            COWArray(0x  AA2E40[0..4]) @ 1
> this(this);               COWArray(0x  AA2E40[0..4]) @ 1
< this(this);               COWArray(0x  AA2E40[0..4]) @ 2
a: [1,2,3,4]
> ~this();                  COWArray(0x  AA2E40[0..4]) @ 2
< ~this();                  COWArray(0x  AA2E40[0..4]) @ 1

For the following code:

void main()
    COWArray!(int) a;
    a = [1,2,3,4];

    // This shouldn't affect ref counts
    auto a_str = a.toString;
    writefln("a: %s", a_str);

As you can see, it correctly enters opAssign and leaves with the correct
array reference and ref count.  However, it also spuriously calls the
destructor with what appears to be a random pointer.  It then calls the
postblit when it shouldn't be doing so, resulting in the refcount being
one too high.

I've attached the full source (compile with -debug); does anyone know
what I'm doing wrong, or whether this is a compiler bug?

  -- Daniel

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: cow_bug.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20090318/bc6a3c74/attachment.asc>

More information about the Digitalmars-d-learn mailing list