www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - scope classes on heap

test2() instantiates Bar on the heap.
Should it be allowed to do so? Bar is a scope class...

scope class Bar {
    long[10000] lng;
    ~this(){
        writefln("dtor! ");
    }
    delete(void* mem){
        writefln("del (heap)!");
    }
    new(uint size){
        writef("new (heap)! ");
        return cast(void*)malloc(size);
    }
}
void test(){
    std.gc.disable();
    while(true){
        test2();
    }
}
void test2(){
    //scope Bar b = new Bar(); // OK: Bar "explicitly" on stack
    //foo(b); // OK: Bar is on stack... deleted when function returns
    //Bar b = new Bar(); // ERROR: Bar "explicitly" on heap
    foo(new Bar()); // But... OK??? Bar "implicitly" on heap
}
void foo(Bar bar){
    writefln("scope bar: ", cast(Bar*)bar);
}
Oct 21 2007