Fix #178: Remove circular reference during cleanup

This commit is contained in:
Zach Borboa
2015-04-22 23:42:35 -07:00
parent c2558f5dd4
commit 91b94627d1
2 changed files with 35 additions and 0 deletions
+1
View File
@@ -133,6 +133,7 @@ class Curl
if (is_resource($this->curl)) {
curl_close($this->curl);
}
$this->options = null;
$this->json_decoder = null;
}
+34
View File
@@ -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);
}
}
}
}