The last block of unit tests:
unittest
{
alias RC = common.RC;
Array!RC ary;
size_t cnt;
assert(cnt == 0);
ary.insertBack(RC(&cnt));
assert(cnt == 1);
ary.insertBack(ary.front); // <---
assert(cnt == 2);
ary.popBack();
assert(cnt == 1);
ary.popBack();
assert(cnt == 0);
}
The marked line takes the front element of array and passes it by reference to ary.insertBack. The problem is that insertBack can realloc the array which can cause it to be moved, invalidating the reference. I'm seeing this happen and resulting in a segfault. For evidence of the problem, changing Array.insertBack to:
void insertBack()(auto ref T val)
{
static if (is(T == common.RC)) printf("val._cnt: %p\n", val._cnt); .fflush(.stdout);
length = length + 1;
static if (is(T == common.RC)) printf("val._cnt: %p\n", val._cnt); .fflush(.stdout);
back = val;
}
val._cnt: 00000000001AF960 (matches &cnt)
val._cnt: 00000000001A003C (doesn't match)
Adding more printf's to clarify the flow between those two points:
Array.length _ptr before xrealloc: 0000000000312900
Array.length _ptr after xrealloc: 0000000000312A60
RC.length grow
common.initialize &t: 0000000000312A70, t._cnt: 0000000000000000
common.initialize memset
Not sure what caused the old _cnt pointer to get overwritten with a new value, but it obviously was, thankfully exposing the problem. The code certainly _looks_ innocent enough. It took way more digging than it should have for me to realize what the problem is. I hate to just remove the test case though.
Maybe replace:
ary.insertBack(ary.front);
with:
ary.insertBack(RC(&cnt));
assert(cnt == 2);
ary.back = ary.front;
assert(cnt == 2);
Comment #1 by github-bugzilla — 2015-03-18T08:03:10Z