From 91b94627d1dd8cf0230043b497b5a7bd788cfcae Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 22 Apr 2015 23:42:35 -0700 Subject: [PATCH] Fix #178: Remove circular reference during cleanup --- src/Curl/Curl.php | 1 + tests/PHPCurlClass/PHPCurlClassTest.php | 34 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 970f567..acc857f 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -133,6 +133,7 @@ class Curl if (is_resource($this->curl)) { curl_close($this->curl); } + $this->options = null; $this->json_decoder = null; } diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 5ccaf77..1828003 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -1282,4 +1282,38 @@ class CurlTest extends PHPUnit_Framework_TestCase Helper\test($test, 'OPTIONS', 'DELETE'); Helper\test($test, 'OPTIONS', 'HEAD'); } + + public function testMemoryLeak() + { + ob_start(); + echo '['; + for ($i = 0; $i < 10; $i++) { + if ($i >= 1) { + echo ','; + } + echo '{"before":' . memory_get_usage() . ','; + $curl = new Curl(); + $curl->close(); + echo '"after":' . memory_get_usage() . '}'; + sleep(1); + } + echo ']'; + $html = ob_get_contents(); + ob_end_clean(); + $results = json_decode($html, true); + + // Ensure memory does not leak excessively after instantiating a new + // Curl instance and cleaning up. Memory diffs in the 2000-6000+ range + // have indicated a memory leak. + $max_memory_diff = 1000; + foreach ($results as $i => $result) { + $memory_diff = $result['after'] - $result['before'];; + echo 'diff: ' . $memory_diff . "\n"; + + // Skip the first test to allow memory usage to settle. + if ($i >= 1) { + $this->assertLessThan($max_memory_diff, $memory_diff); + } + } + } }