Permalink
Browse files

Moving xml_parser_free() so parser resources are freed immediately af…

…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 b559be58221537b220f4bfd1a127e4914e22a349
Showing with 23 additions and 5 deletions.
  1. +3 −5 cake/libs/xml.php
  2. +20 −0 cake/tests/cases/libs/xml.test.php
View
@@ -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;
@@ -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;
}
@@ -922,6 +921,8 @@ function parse() {
break;
}
}
+ xml_parser_free($this->__parser);
+ $this->__parser = null;
return true;
}
/**
@@ -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);
}
/**
@@ -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.