Edit report at https://bugs.php.net/bug.php?id=53228&edit=1

 ID:                 53228
 Comment by:         jille at hexon dot cx
 Reported by:        kgrecki at gmail dot com
 Summary:            memory leak when throwing exception in custom
                     handler in functions
 Status:             Open
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.3.3
 Block user comment: N
 Private report:     N

 New Comment:

This is not a bug. If you unset($e); you don't leak any memory.

It is because exceptions include a backtrace, containing all the arguments 
given to the error handling closure. The fifth argument given is $context, an 
array containing all local variables, including the previous $e.


Previous Comments:
------------------------------------------------------------------------
[2010-11-04 17:14:07] uramihsayibok at gmail dot com

unset($e)ing in the catch block plugs the hole - the previous value/exception 
in 
$e isn't being destroyed?

------------------------------------------------------------------------
[2010-11-02 18:48:12] kgrecki at gmail dot com

Description:
------------
I encountered a similar issue to #25335 where custom error handler + exceptions 
leaks memory but only when try-catch block is in a function and looped through 
directly. I imagine there's some scope/GC issues here, and whether this is 
actual 
bug or limitation of PHP.

Test script:
---------------
<?
set_error_handler(function($errno, $errstr, $errfile, $errline){
    throw new Exception($errstr);
});


function leak() {
    while (true) {
        try {
//            throw new Exception(); //it doesn't leak if it's thrown directly
            1 / 0; //leaks
        } catch (Exception $e) {}
        echo memory_get_usage(), PHP_EOL;
    }
}

leak();

/* it doesn't leak if it's outside of the function */
 
while (true) {
    try {
        1 / 0;
    } catch (Exception $e) {
    }
    echo memory_get_usage(), PHP_EOL;
}

/* it doesn't leak if try-catch block is in a separate function */

function foo() {
    $try = function(){
        try {1 / 0;} catch (Exception $e) {}
    };
    while (true) {
        $try();
        echo memory_get_usage(), PHP_EOL;
    }
}

foo();


Expected result:
----------------
637512
637512
637512
637512
637512
637512
...

Actual result:
--------------
639432
644272
649080
653896
658704
663512
668320
...


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=53228&edit=1

Reply via email to