[Issue 1765] New: foreach scope does not call destructor
    d-bugmail at puremagic.com 
    d-bugmail at puremagic.com
       
    Wed Jan  2 10:59:51 PST 2008
    
    
  
http://d.puremagic.com/issues/show_bug.cgi?id=1765
           Summary: foreach scope does not call destructor
           Product: D
           Version: 2.008
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: gide at nwawudu.com
Foreach statement (without braces '{}') and scoping objects, prevents the call
to the object's destructor. The code below outlines the problem.
Code
----
module main;
import std.stdio;
class MyClass {
    public:
        this()    {   
            writefln("Constructor");
        }
        ~this()   {   
            writefln("Destructor"); 
        }
    private:
        int myNumber;
};
int main() {
    writefln("1 - Start: OK dtor called");
    foreach (a; new int[1]) {
        scope x = new MyClass;
    }
    writefln("1 - End:   OK dtor called\n");
    writefln("2 - Start: dtor NOT called");
    foreach (a; new int[1])
        scope x = new MyClass;
    writefln("2 - End:   dtor NOT called\n");
    return 0;
};
Output
------
1 - Start: OK dtor called
Constructor
Destructor
1 - End:   OK dtor called
2 - Start: dtor NOT called
Constructor
2 - End:   dtor NOT called
-- 
    
    
More information about the Digitalmars-d-bugs
mailing list