Edit report at https://bugs.php.net/bug.php?id=29368&edit=1
ID: 29368 Updated by: s...@php.net Reported by: fixxxer at php5 dot ru Summary: The destructor is called when an exception is thrown from the constructor -Status: Closed +Status: Re-Opened Type: Bug Package: Scripting Engine problem Operating System: * -PHP Version: 5.0.0 +PHP Version: 5.4.0 Assigned To: helly Block user comment: N Private report: N New Comment: This bug is still reproducible in 5.4 with this code: <?php function throwme($arg) { throw new Exception; } class foo { function __construct() { echo "Inside constructor\n"; throwme($this); } function __destruct() { echo "Inside destructor\n"; } } try { $bar = new foo; } catch(Exception $exc) { echo "Caught exception!\n"; } This produces: Inside constructor Caught exception! Inside destructor This is because the object is kept as part of "args" in the exception's backtrace. Better solution is needed here, the one in f5cf052225ff4bc5ba7fe2c8b9f0ffcd980cac6f does not solve the whole issue. Previous Comments: ------------------------------------------------------------------------ [2004-09-24 16:43:45] ernest at vogelsinger dot at This bug still (again?) exists in 5.0.2/Win32! ------------------------------------------------------------------------ [2004-07-25 19:39:06] he...@php.net This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. ------------------------------------------------------------------------ [2004-07-24 22:43:10] fixxxer at php5 dot ru Description: ------------ The destructor is called if throwing an exception from the constructor. This seems at least illogical and it's contrary to usual behaviour of alike languages like C++ where destructor is not called in this case. Reproduce code: --------------- <? class foo { function __construct() { echo "Inside constructor\n"; throw new Exception; } function __destruct() { echo "Inside destructor\n"; } } try { $bar = new foo; } catch(Exception $exc) { echo "Caught exception!\n"; } ?> Expected result: ---------------- Inside constructor Caught exception! Actual result: -------------- Inside constructor Inside destructor Caught exception! ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=29368&edit=1