Nested `CallbackFilterIterator` is leaking memory
Description
The following code:
https://3v4l.org/3nIOH
https://3v4l.org/YQIVJ/rfc#vgit.master
is expected to output:
-- c 1
Array
(
[0] => a
[1] => b
)
-- d 1
-- c 1
Array
(
[0] => a
[1] => b
)
-- d 1
-- c 1
Array
(
[0] => a
)
+ -- d 1
----------------
- -- d 1
PHP Version
8.0.14, 8.1.1, master (all tested)
Operating System
linux
Can anyone from PHP team confirm this issue? It is probably related to https://github.com/php/php-src/commit/afab9eb48c883766b7870f76f2e2b0a4bd575786 , the issue with CallbackFilterIterator is however fixed, thanks @nikic
Doesn't seem like this is caused by afab9eb48c883766b7870f76f2e2b0a4bd575786. In fact, it fixes the second iteration in your example, which also leaked.
Here's a somewhat condensed example:
class Action
{
public \Iterator $iterator;
public function __construct(array $data)
{
$this->iterator = new ArrayIterator($data);
echo '-- c ' . spl_object_id($this) . "\n";
}
public function __destruct()
{
echo '-- d ' . spl_object_id($this) . "\n";
}
public function filter()
{
$this->iterator = new \CallbackFilterIterator($this->iterator, fn() => true);
$this->iterator->rewind();
}
}
$action = new Action(['a', 'b']);
$action->filter();
$action->filter();
print_r(iterator_to_array($action->iterator));
$action = null;
gc_collect_cycles();
-- c 1
Array
(
[0] => a
[1] => b
)
===DONE===
-- d 1
[Thu Feb 3 15:51:02 2022] Script: './ext/spl/tests/bug-gh7958.php'
./Zend/zend_closures.c(505) : Freeing 0x00007f5edee6b900 (328 bytes), script=./ext/spl/tests/bug-gh7958.php
[Thu Feb 3 15:51:02 2022] Script: './ext/spl/tests/bug-gh7958.php'
./Zend/zend_objects_API.h(92) : Freeing 0x00007f5edee84540 (176 bytes), script=./ext/spl/tests/bug-gh7958.php
[Thu Feb 3 15:51:02 2022] Script: './ext/spl/tests/bug-gh7958.php'
./Zend/zend_interfaces.c(206) : Freeing 0x00007f5edee87280 (112 bytes), script=./ext/spl/tests/bug-gh7958.php
=== Total 3 memory leaks detected ===
It's caused by the reference to $this from the CallbackFilterIterator closure. Making the closure static solves the issue. It only happens for nested CallbackFilterIterator iterators.
It's caused by the reference to
$thisfrom theCallbackFilterIteratorclosure. Making the closurestaticsolves the issue. It only happens for nestedCallbackFilterIteratoriterators.
Yes, it seems ref count is not properly decremented during GC cycle when CallbackFilterIterator is nested - https://3v4l.org/mNt0C/rfc#vgit.master
Can you please mark this issue as verified and do you think it can be fixed in PHP v8.0 or at least v8.1?