Weird behavior: array of RefCounted struct

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 9 09:46:30 PDT 2014


On Tue, 09 Sep 2014 16:13:24 +0000
Xaqq via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> Am I doing something wrong or is this a bug? Could someone please 
> try to explain?
ah, this is a very funny thing. you are using D dynamic array to keep
refcounted objects and by appending element to this array you are
hitting "array resizing". and from that moment everything goes wrong:
compiler needs to copy old array elements to new resized array. and
copying increases refcount. yet compiler doesn't know about this and
doesn't call destructors for copied elements (so refcount not
decreasing). this is perfectly ok for garbage-collected elements, but
completely wrong for refcounted elements.

to make a long story short: don't store RefCounted in dynamic arrays.
this will not work as expected yet.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140909/90f4867a/attachment.sig>


More information about the Digitalmars-d-learn mailing list