|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-03-08 05:38 UTC] james dot harris at icecave dot com dot au
Description: ------------ A memory leak occurs when a closure that is defined inside a class and has a parameter named $this is invoked with a value that contains a reference to $this. This gist (https://gist.github.com/jmalloc/e3db5842c2c4ab2a1edf) might help explain a little better. Affected PHP versions: https://3v4l.org/lr963 Test script: --------------- class MemoryLeak { public function bad() { $closure = function ($this) {}; $closure([$this]); } } $object = new MemoryLeak; ini_set('memory_limit', '10M'); for ($i = 0; $i < 1000000; ++$i) { $object->bad(); } echo "Done" . PHP_EOL; Expected result: ---------------- Done Actual result: -------------- Fatal error: Allowed memory size of 10485760 bytes exhausted (tried to allocate 4096 bytes) in /Users/james/<snip>/leak.php on line 8 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 19:00:01 2025 UTC |
This should be just outright forbidden as Closures may be bound to a $this and we generally do prevent $this overloading in methods. php -r 'class a { function b($this) {} } (new a)->b(1);' PHP Fatal error: Cannot re-assign $this in Command line code on line 1