php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #14037 domxml: Memory leak in xmldocfile function
Submitted: 2001-11-12 17:01 UTC Modified: 2002-08-10 18:44 UTC
From: mikep at oeone dot com Assigned: mfischer (profile)
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.0.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mikep at oeone dot com
New email:
PHP Version: OS:

 

 [2001-11-12 17:01 UTC] mikep at oeone dot com
<?php
echo "starting test ...<p>";
flush();
for ($i=0; $i<100; $i++)
{
        $doc = xmldocfile( "SOMEXMLFILE" );
}
echo "test finished";
?>

When this is done on Apache, the memory used by Apache sky rockets.  The larger the XML file, the larger the memory leak.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-29 09:38 UTC] mfischer@php.net
What do you define as 'memory leak' ? Error output in apaches log?

Feedback.
 [2001-11-29 09:42 UTC] mikep at oeone dot com
As stated, the memory used by Apache sky rockets... On my machine, it goes up to about 90% or so.
We're working on a patch to this bug, we've got the memory leak fixed by closing the file using a function we wrote, but unfortunately that ended up causing a segfault in Apache. 
When we fix that problem, we'll post the fix here.
 [2001-11-29 09:48 UTC] mfischer@php.net
By patching what? ext/domxml?

FYI, apache doesn't release memory it once grabs. Thats how it works.

Feedback.
 [2001-11-29 11:29 UTC] mikep at oeone dot com
Here's the explanation from mostafah@oeone.com (our resident C expert):
In the ext/domxml/php_domxml.c there is a destructor function named php_free_xml_doc(). In the loop code that mikep@oeone.com has posted this destructor function gets invoked after the end of the loop which for some reason won't free up memory. We thought maybe the xmldoc memory
should be released within the loop that is after getting an xmldoc object into $doc and before overwriting it through the next iteration of the loop. So we added our own xmldocfree( $doc ) function and put it in the loop. We had no memory leak anymore but then we encountered segfaults in the apache log file which of course was because of the native destructor being invoked on an already released object. So maybe even our xmldocfree wasn't working properly and the segfault was somehow cleaning up everything behind. But anyway we are happy now because the
memory leak is gone and the segfault does not effect our functionality.
 [2001-11-29 13:39 UTC] mikep at oeone dot com
I need a way to attach a file to this bug...
Here is the diff:

diff -ur php-4.0.6/ext/domxml/php_domxml.c php-4.0.6-haxx0red/ext/domxml/php_domxml.c
--- php-4.0.6/ext/domxml/php_domxml.c	Thu May 24 08:41:46 2001
+++ php-4.0.6-haxx0red/ext/domxml/php_domxml.c	Tue Nov 13 17:26:03 2001
@@ -71,6 +71,7 @@
 	PHP_FE(domxml_set_attribute,	NULL)
 	PHP_FALIAS(domxml_setattr,	domxml_set_attribute, NULL)
 	PHP_FE(domxml_children,	NULL)
+	PHP_FE(xmldocfree,	NULL) //oeone
 	PHP_FE(domxml_new_child,	NULL)
 	PHP_FE(domxml_node,	NULL)
 	PHP_FE(domxml_unlink_node,	NULL)
@@ -205,7 +206,7 @@
   domxmltestnode_class_startup();
 #endif
 
-	le_domxmldocp = zend_register_list_destructors_ex(php_free_xml_doc, NULL, "domxml document", module_number);
+	le_domxmldocp = zend_register_list_destructors_ex(php_free_xml_doc, php_free_xml_doc, "domxml document", module_number);//oeone
 	/* Freeing the document contains freeing the complete tree.
 	   Therefore nodes, attributes etc. may not be freed seperately.
 	*/
@@ -1161,6 +1162,38 @@
 	zend_list_addref(ret);
 }
 /* }}} */
+
+//oeone
+PHP_FUNCTION(xmldocfree)
+{
+	zval *id, **tmp;
+	xmlDoc *docp;
+	xmlNode *node;
+	int ret;
+	
+    /* php_error( E_WARNING, "Oeone Destructor\n" ); */
+	if (ZEND_NUM_ARGS() == 0) {
+		id = getThis();
+		if (id) {
+			if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) {
+				php_error(E_WARNING, "unable to find my handle property");
+				RETURN_FALSE;
+			}
+			ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp)
+		} else {
+			RETURN_FALSE;
+		}
+	} else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) {
+		WRONG_PARAM_COUNT;
+	} else {
+		if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) {
+			php_error(E_WARNING, "unable to find my handle property");
+			RETURN_FALSE;
+		}
+		ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp)
+	}
+	xmlFreeDoc(docp);
+}
 
 /* {{{ proto object domxml_new_child([int node_handle,] string name, string content)
    Adds child node to parent node */
diff -ur php-4.0.6/ext/domxml/php_domxml.h php-4.0.6-haxx0red/ext/domxml/php_domxml.h
--- php-4.0.6/ext/domxml/php_domxml.h	Thu May 24 08:33:43 2001
+++ php-4.0.6-haxx0red/ext/domxml/php_domxml.h	Tue Nov 13 17:26:03 2001
@@ -46,6 +46,7 @@
 PHP_FUNCTION(domxml_add_root);
 PHP_FUNCTION(domxml_intdtd);
 PHP_FUNCTION(domxml_dumpmem);
+PHP_FUNCTION(xmldocfree); //oeone
 
 /* Class Node methods */
 PHP_FUNCTION(domxml_attributes);

 [2001-11-29 19:05 UTC] mfischer@php.net
It probably segfaults becaue you don't delete the resource from the list and so it gets double freed which results in a crash (didn't looked thorougly though).

The memory all gets freed upon scrip terrmination but I see the advantage of freeing the resources when no longer required.

Assigned to me.
 [2001-12-11 17:25 UTC] mfischer@php.net
De-assigned, makeing it a feature request.
 [2002-08-10 18:44 UTC] kalowsky@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 [2003-10-10 13:43 UTC] luiz dot pestana at elementstudio dot com dot br
I solved the problem by adding the declaration of function  "domxml_doc_free_doc" in the file "ext/domxml/php_domxml.c".

260 - PHP_FE(domxml_parser_end_document,									NULL)
261	- PHP_FE(domxml_parser_get_document,									NULL)
262 - PHP_FE(domxml_doc_free_doc,									NULL) // here
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC