Bug #80125 [Nab->Dup]: CallbackFilterIterator is leaking memory

From: Date: Wed, 23 Sep 2020 20:08:19 +0000
Subject: Bug #80125 [Nab->Dup]: CallbackFilterIterator is leaking memory
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229189@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80125&edit=1

 ID:                 80125
 Updated by:         nikic@php.net
 Reported by:        mvorisek at mvorisek dot cz
 Summary:            CallbackFilterIterator is leaking memory
-Status:             Not a bug
+Status:             Duplicate
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   linux
 PHP Version:        7.4.10
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of bug #65387.


Previous Comments:
------------------------------------------------------------------------
[2020-09-23 20:04:43] mvorisek at mvorisek dot cz

this is not working as well, Closure::bindTo is defined like "duplicate and bind", so you
must assign the result

https://3v4l.org/Ggt54

then the issue remains - CallbackFilterIterator is not GCed even if there is no reference to it, it
is a clear bug to me and it should be fixed

------------------------------------------------------------------------
[2020-09-23 16:45:14] kalle@php.net

Then create a closure, and pass it, clone + bindTo on assignment it to the scope which you want:

https://3v4l.org/gemWn

------------------------------------------------------------------------
[2020-09-23 16:27:29] mvorisek at mvorisek dot cz

No! The CallbackFilterIterator must be GCable like any other object.

I simplified the test script (to use bound function with simple condition), I can not use static and
I also mentioned this in the description. This is a clear bug to me.

------------------------------------------------------------------------
[2020-09-23 16:19:36] kalle@php.net

You should declare the closure as static and you will get the desired result:
https://3v4l.org/QPlkq

------------------------------------------------------------------------
[2020-09-20 13:12:33] mvorisek at mvorisek dot cz

Description:
------------
see test script, explicit GC does not help

the issues seems present when the given callback to CallbackFilterIterator is bound (eg. not static)

https://3v4l.org/UpmHK

Test script:
---------------
class IteratorWithFilter
{
    public $generator;

    public function __construct(array $data)
    {
        $this->generator = new \ArrayIterator($data);
    }

    public function filter()
    {
        $this->generator = new \CallbackFilterIterator($this->generator, function ($row)  {
            return true;
        });
        
        return $this;
    }
}

function createIterator() {
    $iter = new IteratorWithFilter([['a'], ['b']]);
    return $iter->filter();
}

for ($i = 0; $i < 50 * 1000; $i++) {
    createIterator();
    
    if (($i % 1000) === 0) {
        // gc_collect_cycles(); // explicit GC does not help...
        echo round(memory_get_usage() / (1024 * 1024), 3) . " MiB\n";
    }
}

Expected result:
----------------
reported memory usage should stay below 1 MiB

Actual result:
--------------
0.375 MiB
1.719 MiB
3.071 MiB
4.36 MiB
5.774 MiB
7.064 MiB
8.353 MiB
9.892 MiB
...


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



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


Thread (7 messages)

« previous php.bugs (#229189) next »