www.digitalmars.com         C & C++   DMDScript  

D - [BUG] gc & static

reply Sark7 <sark7 mail333.com> writes:
[code]
class C {
   this() {
     printf("ctor"\n);
   }
   ~this() {
     printf("dtor"\n);
   }
}

void main() {
   static C c;
   c = new C();
}
[/code]

If c is not static, dtor is called.
But why dtor for 'c' is not called, when it is static ?
Apr 24 2004
parent reply "Walter" <walter digitalmars.com> writes:
"Sark7" <sark7 mail333.com> wrote in message
news:opr6yy0lvjut8jae news.digitalmars.com...
 But why dtor for 'c' is not called, when it is static ?
It should be.
Apr 24 2004
parent Craig Pennington <cpenning milo.org> writes:
Walter wrote:
 "Sark7" <sark7 mail333.com> wrote in message
 news:opr6yy0lvjut8jae news.digitalmars.com...
But why dtor for 'c' is not called, when it is static ?
 It should be.
Should it be? What is the purpose of declaring the instance static? If it is, as I would expect (and I am admittedly brand-spanking new to D) in any other function aside from main, to create a persistent variable then I am not sure you would want to call the dtor for that variables class instance on the function exit (I think that you would not if the ctor/dtor were bypassing GC.) Perhaps it should be called as part of the post-main clean-up as might happen (but doesn't actually on testing with dmd and dgc) with global variables. Of course, it doesn't really matter, because the one-line declaration/assignment generates errors for both global and persistent class instances. i.e. [code] class C { int x; this() { printf("ctor"\n); x=0; } ~this() { printf("dtor"\n); } void incx() { x=x+1; } void px() { printf("x=%d"\n, x); } } void foo() { static C c = new C(); static int i = 0; c.incx(); c.px(); i++; printf("i=%d"\n,i); } void main() { foo(); foo(); } [/code] Barfs on compile (both dmd and gdc, so it's the gcc backend complaining) with: [error] test.d: In function `foo': test.d:0: internal compiler error: output_operand: invalid expression as operandPlease submit a full bug report, with preprocessed source if appropriate. See <URL:http://gcc.gnu.org/bugs.html> for instructions. [/error] Whereas I was expecting: [expected] ctor x=1 i=1 x=2 i=2 [/expected] NB: Global variable ctor/dtor behavior: [code] class A { this() { printf("A ctor"\n); } ~this() { printf("A dtor"\n); } } class G { this() { printf("G ctor"\n); } ~this() { printf("G dtor"\n); } } G g; void foo() { g = new G(); } int main(char[][] argv) { foo(); A a = new A(); return 0; } [/code] [results] G ctor A ctor A dtor [/results] Given the behavior of the global variable, and given that my reading of the use of the "static" keyword in the given context generates a variable accessable locally but that otherwise behaves as a global variable, I would consider the failure to call the dtor expected. If it is a problem (and I think it may be a problem;) I think it is with the cleanup after (or at the end of) the execution of main, not specifically with persistent class instances. Cheers, Craig
Apr 25 2004