Skip to content

Fix memory leak with OCI_RETURN_LOBS fetch flag #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions oci8.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,8 @@ void php_oci_column_hash_dtor(zval *data)
zend_list_close(column->stmtid);
}

if (column->descid) {
if (GC_REFCOUNT(column->descid) == 1)
zend_list_close(column->descid);
else {
GC_DELREF(column->descid);
}
if (column->descid && !GC_DELREF(column->descid)) {
zend_list_free(column->descid);
}

if (column->data) {
Expand Down
43 changes: 43 additions & 0 deletions tests/gh-4-return-lob.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Bug GH-4 (Memory leak with long query/CLOB) with OCI_RETURN_LOBS
--EXTENSIONS--
oci8
--SKIPIF--
<?php
require_once 'skipifconnectfailure.inc';
?>
--FILE--
<?php

require __DIR__.'/connect.inc';

$expectedStr = str_repeat('a', 1_001);
$sql = 'select concat(TO_CLOB(\'' . str_repeat('a', 1_000) . '\'), TO_CLOB(\'a\')) AS "v" from "DUAL"';

$memUsages = array_flip(range(0, 100 - 1));
foreach (array_keys($memUsages) as $k) {
$stid = oci_parse($c, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, \OCI_ASSOC | \OCI_RETURN_LOBS);
$res = $row['v'];
if ($res !== $expectedStr) {
var_dump([$expectedStr, $res]);
throw new \Exception('unexpected result');
}

$memUsages[$k] = memory_get_usage();
}

$memUsages = array_slice($memUsages, 1, null, true);
$memUsages = array_unique($memUsages);

if (count($memUsages) !== 1) {
var_dump($memUsages);
throw new \Exception('memory leak detected');
}

echo "ok\n";

?>
--EXPECTF--
ok