Jump to content

Circular References Memory Leak - Fatal Error: Call to a member function...


soma56

Recommended Posts

S.C. Chen created a great script known as PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net). It allows for easy manipulation of html. While running the script through a loop I've been experiencing major memory issues. I soon discovered that the script has a function that cleans up the memory. Great! Here's a tidbit from the Author:

 

Due to php5 circular references memory leak, after creating DOM object, you must call $dom->clear() to free memory if call file_get_dom() more then once. 

Example: 

$html = file_get_html(...); 
// do something... 
$html->clear(); 
unset($html);

 

This seems simple enough. I'm converting the html to plaintext through his script so I figured this would work:

 

include('simple_html_dom.php');
$html = file_get_html(...); 
// do something... 
$text = str_get_dom($html)->plaintext;
// do something... 
$text->clear(); 
unset($html);

 

I receive the following error "Call to a member function clear() on a non-object in...". This appears to be the results of failing to include the script that contains the function yet other aspects of the script work correctly. This is based on my 2 months of learning php anyways. I suspect I'm not calling the function correctly $text->clear(); or is there something I'm missing?

 

I appreciate the quick response. I tried that and received the same results. Googling this references a lot of SQL issues. I did find one line that I'm thinking might relate and I'm trying to comprehend:

 

Your constructor for the class is not setting the object member variable correctly, so it remains a null reference, which causes the error.

There are two 'clear' functions within the Simple HTML DOM Parser script:

 

class simple_html_dom_node {
    // clean up memory due to php5 circular references memory leak...
    function clear() {
        $this->dom = null;
        $this->nodes = null;
        $this->parent = null;
        $this->children = null;
    }
}



class simple_html_dom {
    function clear() {
        foreach($this->nodes as $n) {$n->clear(); $n = null;}
        if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
        if (isset($this->root)) {$this->root->clear(); unset($this->root);}
        unset($this->doc);
        unset($this->noise);
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.