diff --git a/cake/libs/xml.php b/cake/libs/xml.php index 5c8cb40c3..c61cfa0c0 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -691,7 +691,6 @@ class XmlNode extends Object { foreach ($this->children as $child) { $key = $camelize ? Inflector::camelize($child->name) : $child->name; - //debug($key); if (is_a($child, 'XmlTextNode')) { $out['value'] = $child->value; @@ -873,7 +872,7 @@ class Xml extends XmlNode { parent::__construct('#document'); if ($options['root'] !== '#document') { - $Root = $this->createNode($options['root']); + $Root =& $this->createNode($options['root']); } else { $Root =& $this; } @@ -955,6 +954,8 @@ class Xml extends XmlNode { break; } } + xml_parser_free($this->__parser); + $this->__parser = null; return true; } @@ -1132,9 +1133,6 @@ class Xml extends XmlNode { * @access private */ function __destruct() { - if (is_resource($this->__parser)) { - xml_parser_free($this->__parser); - } $this->_killParent(true); } diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index aa237fb07..c8358210a 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -1396,5 +1396,25 @@ class XmlTest extends CakeTestCase { $result = $result[0]->first(); $this->assertEqual($result->value, '012345'); } + +/** + * test that creating an xml object does not leak memory + * + * @return void + */ + function testMemoryLeakInConstructor() { + if ($this->skipIf(!function_exists('memory_get_usage'), 'Cannot test memory leaks without memory_get_usage')) { + return; + } + $data = 'TEST'; + $start = memory_get_usage(); + for ($i = 0; $i <= 300; $i++) { + $test =& new XML($data); + $test->__destruct(); + unset($test); + } + $end = memory_get_usage(); + $this->assertWithinMargin($start, $end, 3600, 'Memory leaked %s'); + } } ?> \ No newline at end of file