Skip to content

Commit

Permalink
Moving xml_parser_free() so parser resources are freed immediately af…
Browse files Browse the repository at this point in the history
…ter they are used. Helps reduce memory consumption in Xml class. Refs #505
  • Loading branch information
markstory committed Mar 27, 2010
1 parent abefca7 commit b559be5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cake/libs/xml.php
Expand Up @@ -664,7 +664,6 @@ function toArray($camelize = true) {

foreach ($this->children as $child) {
$key = $camelize ? Inflector::camelize($child->name) : $child->name;
//debug($key);

if (is_a($child, 'XmlTextNode')) {
$out['value'] = $child->value;
Expand Down Expand Up @@ -842,7 +841,7 @@ function __construct($input = null, $options = array()) {
parent::__construct('#document');

if ($options['root'] !== '#document') {
$Root = $this->createNode($options['root']);
$Root =& $this->createNode($options['root']);
} else {
$Root =& $this;
}
Expand Down Expand Up @@ -922,6 +921,8 @@ function parse() {
break;
}
}
xml_parser_free($this->__parser);
$this->__parser = null;
return true;
}
/**
Expand Down Expand Up @@ -1089,9 +1090,6 @@ function header($attrib = array()) {
* @access private
*/
function __destruct() {
if (is_resource($this->__parser)) {
xml_parser_free($this->__parser);
}
$this->_killParent(true);
}
/**
Expand Down
20 changes: 20 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -1370,5 +1370,25 @@ function testNumericDataHandling() {
$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 = '<?xml version="1.0" encoding="UTF-8"?><content>TEST</content>';
$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');
}
}
?>

0 comments on commit b559be5

Please sign in to comment.