From b8e1bdbed4c29896a462a2a00ba55c29074a2103 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 26 Sep 2020 15:11:47 +0100 Subject: [PATCH] MemoryManager: Look inside Closure objects to resolve dependencies the lack of closure analysis allowed several memory leaks to be unable to be debugged using memory dumps. --- src/MemoryManager.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/MemoryManager.php b/src/MemoryManager.php index e2c33e2a4..9d7e3d4c8 100644 --- a/src/MemoryManager.php +++ b/src/MemoryManager.php @@ -399,24 +399,37 @@ class MemoryManager{ } $objects[$hash] = true; - - $reflection = new \ReflectionObject($object); - $info = [ "information" => "$hash@$className", - "properties" => [] ]; - - foreach($reflection->getProperties() as $property){ - if($property->isStatic()){ - continue; + if($object instanceof \Closure){ + $info["definition"] = Utils::getNiceClosureName($object); + $info["referencedVars"] = []; + $reflect = new \ReflectionFunction($object); + if(($closureThis = $reflect->getClosureThis()) !== null){ + $info["this"] = self::continueDump($closureThis, $objects, $refCounts, 0, $maxNesting, $maxStringSize); } - if(!$property->isPublic()){ - $property->setAccessible(true); + //TODO: scan non-closures for statics as well + foreach($reflect->getStaticVariables() as $name => $variable){ + $info["referencedVars"][$name] = self::continueDump($variable, $objects, $refCounts, 0, $maxNesting, $maxStringSize); } + }else{ + $reflection = new \ReflectionObject($object); - $info["properties"][$property->getName()] = self::continueDump($property->getValue($object), $objects, $refCounts, 0, $maxNesting, $maxStringSize); + $info["properties"] = []; + + foreach($reflection->getProperties() as $property){ + if($property->isStatic()){ + continue; + } + + if(!$property->isPublic()){ + $property->setAccessible(true); + } + + $info["properties"][$property->getName()] = self::continueDump($property->getValue($object), $objects, $refCounts, 0, $maxNesting, $maxStringSize); + } } fwrite($obData, json_encode($info, JSON_UNESCAPED_SLASHES) . "\n");