Skip to content

Commit 29a1047

Browse files
committed
Merge pull request php-curl-class#179 from zachborboa/master
Remove circular reference during cleanup
2 parents 045669c + 91b9462 commit 29a1047

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Curl/Curl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function close()
133133
if (is_resource($this->curl)) {
134134
curl_close($this->curl);
135135
}
136+
$this->options = null;
136137
$this->json_decoder = null;
137138
}
138139

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,4 +1282,38 @@ public function testRequestMethodSuccessiveOptionsRequests()
12821282
Helper\test($test, 'OPTIONS', 'DELETE');
12831283
Helper\test($test, 'OPTIONS', 'HEAD');
12841284
}
1285+
1286+
public function testMemoryLeak()
1287+
{
1288+
ob_start();
1289+
echo '[';
1290+
for ($i = 0; $i < 10; $i++) {
1291+
if ($i >= 1) {
1292+
echo ',';
1293+
}
1294+
echo '{"before":' . memory_get_usage() . ',';
1295+
$curl = new Curl();
1296+
$curl->close();
1297+
echo '"after":' . memory_get_usage() . '}';
1298+
sleep(1);
1299+
}
1300+
echo ']';
1301+
$html = ob_get_contents();
1302+
ob_end_clean();
1303+
$results = json_decode($html, true);
1304+
1305+
// Ensure memory does not leak excessively after instantiating a new
1306+
// Curl instance and cleaning up. Memory diffs in the 2000-6000+ range
1307+
// have indicated a memory leak.
1308+
$max_memory_diff = 1000;
1309+
foreach ($results as $i => $result) {
1310+
$memory_diff = $result['after'] - $result['before'];;
1311+
echo 'diff: ' . $memory_diff . "\n";
1312+
1313+
// Skip the first test to allow memory usage to settle.
1314+
if ($i >= 1) {
1315+
$this->assertLessThan($max_memory_diff, $memory_diff);
1316+
}
1317+
}
1318+
}
12851319
}

0 commit comments

Comments
 (0)