Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.21.0

Bug #18359 Memory leak in between XML_UnSerializer & XML_Parser
Submitted: 2011-03-11 22:50 UTC
From: felixl Assigned:
Status: Open Package: XML_Serializer (version 0.20.2)
PHP Version: 5.2.12 OS: WinXP
Roadmaps: (Not assigned)    
Subscription  


 [2011-03-11 22:50 UTC] felixl (Felix Labrecque)
Description: ------------ In XML_UnSerializer, you call setHandlerObj. This create a circular reference between xml_unserializer & xml_parser. calling xml_unserializer in a loop create a memory leak because it's never freed. Unsetting _parser in XML_Unserializer before the loop fix the problem (or using setHandlerObj() too) You should restructure the whole code with abstract function, so that we xml_parse call the function handler, the functions are re-defined in XML_Unserializer, so no need for the setHandler function at all. This will definitively fix the problem. Also, maybe php 5.3, with is new garbage collector, can detect and still delete the object. I didn't test it. To be sure that it works, use php >= 5, and create a __destruct() function in XML_Unserializer with a print in it. This function should be called each loop. Also, this problem also exist in XML_Serializer too and any other function that extends XML_Parser. Test script: --------------- <?php error_reporting(E_ALL); ini_set('display_errors', true); $xml = <<<EOT <?xml version="1.0"?> <TrackResponse><Response><TransactionReference></TransactionReference><ResponseStatusCode>1</ResponseStatusCode></Response></TrackResponse>'; EOT; require_once 'XML/UnSerializer.php'; $t = null; for ($i = 0; $i < 1000; $i++) { print "$i:" . (string)memory_get_usage(true) . "\n"; $u = new XML_Unserializer(); $a = $u->unserialize($xml); //$u->_parser->setHandlerObj($t); /** * IMPORTANT * If you don't unset the parser, you have a circular reference, * so PHP will not destruct XML_Unserializer */ //unset($u->_parser); unset($u, $a); sleep(1); } Expected result: ---------------- the memory between loop 0 and 1 should increase, but not afterward. Actual result: -------------- It increase, each 10-15 loops. uncommenting the line 21 (unset($u->_parser);) fix the problem.

Comments

 [2011-03-11 22:52 UTC] felixl (Felix Labrecque)
tested on linux (ubuntu) too. Same problem.