yangdajun / xmpphp

Automatically exported from code.google.com/p/xmpphp
0 stars 0 forks source link

Memory leak when timeout is triggered in processUntil function. Fix. #92

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
To fix, change this code in processUntil function in XMLStream.php.

Original Code:
===============
if(array_key_exists($event_key, $this->until_payload)) {
        $payload = $this->until_payload[$event_key];
        unset($this->until_payload[$event_key]);
        unset($this->until_count[$event_key]);
        unset($this->until[$event_key]);
} else {
        $payload = array();
}

New Code:
===============
if(array_key_exists($event_key, $this->until_payload)) {
        $payload = $this->until_payload[$event_key];
        unset($this->until_payload[$event_key]);
        unset($this->until_count[$event_key]);
        unset($this->until[$event_key]);
} else {
        $payload = array();

                // Add the these lines 
        unset($this->until_payload[$event_key]);
        unset($this->until_count[$event_key]);
        unset($this->until[$event_key]);
}
return $payload;

Original issue reported on code.google.com by avisovoc...@gmail.com on 8 May 2010 at 6:36

GoogleCodeExporter commented 8 years ago
You propose to unset $this->until_payload[$event_key] but array_key_exists() 
just checked that it doesn't exist.
Actually you only need to unset $this->until and $this->until_count arrays.

However my solution is to comment the line:
        $this->until_count[$event_key] = 0;
and to move
            unset($this->until[$event_key]);
out of the condition block to the end of the function, right above the return 
statement.
I think that's bit more correct.

code:
                //$this->until_count[$event_key] = 0;
                //$updated = '';
                while(!$this->disconnected and $this->until_count[$event_key] < 1 and (time() - $start < $timeout or $timeout == -1)) {
                        $this->__process();
                }
                if(array_key_exists($event_key, $this->until_payload)) {
                        $payload = $this->until_payload[$event_key];
                        unset($this->until_payload[$event_key]);
                        unset($this->until_count[$event_key]);
                } else {
                        $payload = array();
                }
                unset($this->until[$event_key]);
                return $payload;

Original comment by tifss...@gmail.com on 4 Aug 2012 at 11:49