scope attribute and catching exception

Max Samukha samukha at voliacable.com.removethis
Mon Oct 13 05:51:21 PDT 2008


On Mon, 13 Oct 2008 08:03:32 -0400, Paolo Invernizzi
<arathorn at fastwebnet.it> wrote:

>Max Samukha Wrote:
>
>> because r2 wasn't successfully created and there is nothing to call
>> the destructor on:
>
>I think is more complicated... see this:
>
>module tests.d.scopes.t02.test;
>
>class Resource {
>    static int allocated = 0;
>    this( bool mustThrow = false ){
>        allocated ++;
>        if( mustThrow ) throw new Exception("bang");
>    }
>    ~this(){
>        allocated --;
>    }
>}
>class Factory {
>    Resource resource(bool b=false){
>        return new Resource(b);
>    }
>}
>
>import std.gc, std.stdio;
>void main(){
>    // In this case everything it's ok
>    {
>        try {
>            scope r1 = new Resource(true);
>            assert( false );
>        }
>        catch(Exception e){}
>        assert( Resource.allocated == 1);
>        std.gc.fullCollect();
>        assert( Resource.allocated == 1 );
>    }
>    
>    Resource.allocated = 0;
>    
>    // In this case the destructor IS called, only on collect...
>    {
>        auto f = new Factory();
>        try {
>            scope r1 = f.resource(true);
>            assert( false );
>        }
>        catch(Exception e){}
>        assert( Resource.allocated == 1);
>        std.gc.fullCollect();
>        assert( Resource.allocated == 0 ); // Dho! Destructor called!
>    }
>}
>

I think this is a bug.
 


More information about the Digitalmars-d-learn mailing list