[php-src] Issue #10043: WeakMap prevents garbage collection
| From: | nicolas-grekas | Date: | Fri, 02 Dec 2022 22:27:11 +0000 |
| Subject: | [php-src] Issue #10043: WeakMap prevents garbage collection | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243036@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10043
Author: nicolas-grekas
### Description
I have a situation where I create an object graph similar to this one:
```php
class Canary extends stdClass
{
public function __construct(public string $name)
{
}
function __destruct()
{
echo $this->name."\n";
}
}
$container = new Canary('container');
$canary = new Canary('canary');
$container->canary = $canary;
$map = new \WeakMap();
$map[$canary] = $container;
echo 1;
unset($container, $canary);
gc_collect_cycles();
echo 2;
```
Resulted in this output:
```
12
container
canary
```
But I expected this output instead:
```
1container
canary
2
```
Commenting the
$map[$canary] = $container; line generates the expected output.
A simpler reproducer is this one:
```
$canary = new Canary('canary');
$map = new \WeakMap();
$map[$canary] = $canary;
echo 1;
unset($canary);
gc_collect_cycles();
echo 2;
```
Which resulted in this output:
```
12
canary
```
But I expected this output instead:
```
1canary
2
```
### PHP Version
PHP >= 8.0
### Operating System
_No response_