[php-src] Issue #7870: Self-reference is counted as circular reference
| From: | woho | Date: | Sun, 02 Jan 2022 14:36:47 +0000 |
| Subject: | [php-src] Issue #7870: Self-reference is counted as circular reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238710@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7870
Author: woho
### Description
A self-reference is regarded as a circular reference with implications on garbage collection. A
real-world usecase where this can be relevant is setting an instance variable to an anonymous
function, which is also a kind of self-reference. Such objects need to be garbage-collected by
gc_collect_cycles when creating many of them, or memory exhaustion can result. Would it be possible
to make the reference counting smarter to avoid this?
```php
<?php
$sr = new SelfRef;
unset($sr);
print("End of script\n");
class SelfRef
{
function __construct()
{
$this->self_ref = $this;
#$this->self_ref = function() { };
}
function __destruct()
{
print("__destruct\n");
}
}
```
Resulted in this output:
```
End of script
__destruct
```
But I expected this output instead:
```
__destruct
End of script
```
### PHP Version
PHP 8.0.14
### Operating System
_No response_