Reference counting doesn't work when temporaries created
application/octet-stream
1792
Comments
Comment #0 by bartosz — 2009-11-16T14:32:39Z
Created attachment 497
Reference counting doesn't work when temporaries created
Function f returns (by value) a struct that has a destructor. If I explicitly
assign the result of f to a temporary, that temporary is destroyed at the end
of scope. If I don't assign it, the hidden temporary is not destroyed. In the
attached example this screws up reference counting.
Comment #1 by bartosz — 2009-11-16T16:31:43Z
*** Issue 3518 has been marked as a duplicate of this issue. ***
Comment #2 by lmb — 2009-11-29T03:21:36Z
I don't know if this is useful information, but I've been bitten by this same bug under Linux with DMD 2.036.
Comment #3 by braddr — 2010-05-30T15:59:09Z
Reduced test case:
extern(C) int printf(const char*, ...);
int numctor, numdtor;
struct Tid
{
this(int i) { ++numctor; }
~this() { ++numdtor; }
}
Tid f() { return Tid(1); }
// This temporary is destroyed
void test1() { Tid tid = f(); }
// This (invisible) temporary is never destroyed
void test2() { f(); }
void main()
{
numctor = numdtor = 0;
test1();
printf("numctor = %d, numdtor = %d\n", numctor, numdtor);
assert(numctor == 1);
assert(numdtor == 1);
numctor = numdtor = 0;
test2();
printf("numctor = %d, numdtor = %d\n", numctor, numdtor);
assert(numctor == 1);
assert(numdtor == 1);
}
Current results:
numctor = 1, numdtor = 1
numctor = 1, numdtor = 0
core.exception.AssertError@bug3516(31): Assertion failure
Comment #4 by lio+bugzilla — 2010-07-06T01:34:59Z
*** Issue 3285 has been marked as a duplicate of this issue. ***
Comment #5 by adrian — 2010-07-10T06:11:10Z
Still active in 2.047,
testcase:
import std.stdio;
struct A
{
int a;
this(int a) { this.a = a; writeln("hello, ", a); }
~this() { writeln("bye, ", a); }
void doSth() { writeln("something"); }
}
void main()
{
auto a1 = A(1);
a1.doSth();
A(2).doSth();
}
produces:
hello, 1
something
hello, 2
something
bye, 1