Bug #53228 [NEW]: memory leak when throwing exception in custom handler in functions

From: Date: Tue, 02 Nov 2010 17:48:13 +0000
Subject: Bug #53228 [NEW]: memory leak when throwing exception in custom handler in functions
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155712@lists.php.net to get a copy of this message
From:             
Operating system: Linux
PHP version:      5.3.3
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:memory leak when throwing exception in custom handler in functions

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 bug report at http://bugs.php.net/bug.php?id=53228&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=53228&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=53228&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=53228&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=53228&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53228&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=53228&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=53228&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=53228&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=53228&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=53228&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=53228&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=53228&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=53228&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=53228&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=53228&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=53228&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=53228&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=53228&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=53228&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=53228&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=53228&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#155712) next »