Skip to content
Open
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
17 changes: 14 additions & 3 deletions leveldb.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,20 @@ PHP_METHOD(LevelDB, __construct)

intern = LEVELDB_OBJ_FROM_ZV(getThis());

if (intern->db) {
leveldb_close(intern->db);
}
if (intern->db) {
leveldb_close(intern->db);
intern->db = NULL;
}

if (intern->comparator) {
leveldb_comparator_destroy(intern->comparator);
intern->comparator = NULL;
}

if (intern->callable_name) {
zend_string_release(intern->callable_name);
intern->callable_name = NULL;
}

openoptions = php_leveldb_get_open_options(options_zv, &intern->comparator, &intern->callable_name);

Expand Down
34 changes: 34 additions & 0 deletions tests/021-reopen.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
leveldb - reopen cleans up old resources
--SKIPIF--
<?php include 'skipif.inc'; ?>
--FILE--
<?php
$path1 = __DIR__ . '/reopen1.test-db';
$path2 = __DIR__ . '/reopen2.test-db';

$db = new LevelDB($path1, array('comparator' => 'custom_comparator'));
$db->set('foo', 'bar');

// Reopen the same object with a new database
$db->__construct($path2);
var_dump($db->get('foo')); // should be false since new DB is empty

function custom_comparator($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
?>
==DONE==
--CLEAN--
<?php
$path1 = __DIR__ . '/reopen1.test-db';
$path2 = __DIR__ . '/reopen2.test-db';
LevelDB::destroy($path1);
LevelDB::destroy($path2);
?>
--EXPECT--
bool(false)
==DONE==