Bug #80951 [NEW]: WeakReferences are collected on scope
From: alex dot a dot pott at gmail dot com
Operating system: OS X / Linux
PHP version: 8.0.3
Package: *General Issues
Bug Type: Bug
Bug description:WeakReferences are collected on scope
Description:
------------
In the WeakReference documentation it says: "Weak references allow the
programmer to retain a reference to an object which does not prevent the
object from being destroyed. They are useful for implementing cache like
structures."
And in the RFC it says:"The application might want to fetch the same
serial number multiple times. Fetching the same product again and making
a new object could have several problems. It might be slow and
expensive."
However the current implementation means thats weak references don't
prevent destruction when an object loses scope. This means that a cache
service that persists objects in memory can't use the WeakReference
object as hoped.
Ideally WeakReferences would prevent an object being destroyed until
garbage collection is run.
For more information see
https://www.drupal.org/project/drupal/issues/3190992#comment-14055154
Test script:
---------------
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
gc_collect_cycles();
var_dump($weakref->get());
Expected result:
----------------
object(stdClass)#1 (0) {
}
object(stdClass)#1 (0) {
}
NULL
Actual result:
--------------
object(stdClass)#1 (0) {
}
NULL
NULL
--
Edit bug report at https://bugs.php.net/bug.php?id=80951&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80951&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80951&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80951&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80951&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80951&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80951&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80951&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80951&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80951&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80951&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80951&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80951&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80951&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80951&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80951&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80951&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80951&r=mysqlcfg
Thread (3 messages)
- alex dot a dot pott at gmail dot com