Bug #66350 [Com]: Custom statement class parameters can cause memory (connection) leak

From: Date: Thu, 14 Jan 2016 22:05:47 +0000
Subject: Bug #66350 [Com]: Custom statement class parameters can cause memory (connection) leak
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198654@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66350&edit=1 ID: 66350 Comment by: noah at noahheck dot com Reported by: sea128 at post dot cz Summary: Custom statement class parameters can cause memory (connection) leak Status: Open Type: Bug Package: PDO Core PHP Version: 5.5.7 Block user comment: N Private report: N New Comment: I experience this same issue using PHP 5.5.9 and 5.6.11 (both on Ubuntu) and mysql (on Ubuntu or CentOS). I've set up a test to replicate the behavior (note the loop is to establish enough connections to hit the max_connections setting for the mysql server). In my tests, I get the expected output, which shows the last connection is still valid, but if the setAttribute line is un-commented, we will get the error message in the catch block. <?php class EPDOStatement extends \PDOStatement { } try { for ($x = 0; $x <= 1000; $x++) { $pdo = new \PDO("mysql:localhost", "test", "test"); // $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("EPDOStatement", array($pdo))); if ($x !== 1000) { $pdo = null; } } } catch (Exception $e) { echo "Exception at number $x: " . $e->getMessage(); exit; } $stmt = $pdo->query("SHOW DATABASES"); print_r($stmt->fetchAll()); Previous Comments: ------------------------------------------------------------------------ [2013-12-25 16:24:26] sea128 at post dot cz Description: ------------ Setting statement class with PDO object as parameter causes cyclic reference which prevent from garbage collection. Test script: --------------- // it is not sqlite bug, similar problem is with postgresql $db = new PDO('sqlite::memory:'); // this line will prevent from garbage collection $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatement', array($db))); // this line will not close connection unset($db); // this line will not close connection too gc_collect_cycles(); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66350&edit=1

« previous php.bugs (#198654) next »