www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - A handful of bugs with exceptions - bugs.zip

So I decided to drop my fire-and-forget mode of bug reporting and organize a
bit.  As such, all of the bugs I report will be tagged by version number and
I'll keep on testing then with each release until they're fixed.  Hopefully
fewer issues will slip through the cracks this way.  Now, on to the bugs...

This report will contain the code for 4-5 related issues--it's likely that
fixing one of these will actually fix a few of them.  I'll paste everything in
standard format, separated by a line.  For convenience, sample code is also
attached in a zipfile.

----------------------------------------------------------------------

D:\code\d\bugs>type 101_1.d
// This demonstrates incorrect behavior for auto class
// construction.  If an exception is thrown out of a
// class ctor then it is an incomplete object and its
// dtor should not be called.

auto class AutoClass
{
public:
this()
{
printf( "ctor\n" );
throw new Exception( "" );
}

~this()
{
printf( "dtor\n" );
}
}


void main()
{
try
{
auto AutoClass c = new AutoClass();
}
catch( Exception e )
{
printf( "caught\n" );
}
}
D:\code\d\bugs>dmd 101_1.d
D:\bin\dmd\bin\..\..\dm\bin\link.exe 101_1,,,user32+kernel32/noi;

D:\code\d\bugs>101_1
ctor
caught
dtor

----------------------------------------------------------------------

D:\code\d\bugs>type 101_2.d
// This demonstrates a delayed dtor call for auto classes
// when an exception is thrown. the dtor should be called
// when execution leaves func(), not after main exits.

auto class AutoClass
{
public:
this()
{
printf( "ctor\n" );
throw new Exception( "" );
}

~this()
{
printf( "dtor\n" );
}
}


void func()
{
auto AutoClass c = new AutoClass();
}


void wrap()
{
try
{
func();
}
catch( Exception e )
{
printf( "caught\n" );
}
}


void main()
{
printf( "main begin\n" );
wrap();
printf( "main end\n" );
}
D:\code\d\bugs>dmd 101_2.d
D:\bin\dmd\bin\..\..\dm\bin\link.exe 101_2,,,user32+kernel32/noi;

D:\code\d\bugs>101_2
main begin
ctor
caught
main end
dtor

----------------------------------------------------------------------

D:\code\d\bugs>type 101_3.d
// This demonstrates a double dtor call for auto classes
// when an exception is thrown in the class dtor.  The
// dtor should only be called once (when func() exits).


auto class AutoClass
{
public:
this()
{
printf( "ctor\n" );
}

~this()
{
printf( "dtor\n" );
throw new Exception( "" );
}
}


void func()
{
auto AutoClass c = new AutoClass();
}


void main()
{
try
{
func();
}
catch( Exception e )
{
printf( "caught\n" );
}
}
D:\code\d\bugs>dmd 101_3.d
D:\bin\dmd\bin\..\..\dm\bin\link.exe 101_3,,,user32+kernel32/noi;

D:\code\d\bugs>101_3
ctor
dtor
caught
dtor
*crash*

----------------------------------------------------------------------

D:\code\d\bugs>type 101_4.d
// This will only be testable once 101_1 through 101_3 have
// been fixed.   Correct behavior should be that the class
// dtor will be called when func() exits (because an
// exception has been thrown), and this dtor's throwing of
// an exception should call terminate().  The other option
// would be to attempt some support for multiple exception
// processing.


auto class AutoClass
{
public:
this()
{
printf( "ctor\n" );
}

~this()
{
printf( "dtor\n" );
throw new Exception( "" );
}
}


void func()
{
auto AutoClass c = new AutoClass();
throw new Exception( "" );
}


void main()
{
try
{
func();
}
catch( Exception e )
{
printf( "caught\n" );
}
}
D:\code\d\bugs>dmd 101_4.d
D:\bin\dmd\bin\..\..\dm\bin\link.exe 101_4,,,user32+kernel32/noi;

D:\code\d\bugs>101_4
ctor
dtor
caught
dtor
*crash*

----------------------------------------------------------------------
Sep 10 2004