Skip to content

Commit

Permalink
Fix mem leak while calling transaction.GetName() (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu authored Oct 13, 2022
1 parent 1f15f77 commit b3ad4ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
6 changes: 0 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,9 +1787,6 @@ func (db *DB) DisableManualCompaction() {
}

// GetColumnFamilyMetadata returns the metadata of the default column family.
//
// Note that the caller is responsible to release the returned memory
// using rocksdb_column_family_metadata_destroy.
func (db *DB) GetColumnFamilyMetadata() (m *ColumnFamilyMetadata) {
if c := C.rocksdb_get_column_family_metadata(db.c); c != nil {
m = newColumnFamilyMetadata(c)
Expand All @@ -1798,9 +1795,6 @@ func (db *DB) GetColumnFamilyMetadata() (m *ColumnFamilyMetadata) {
}

// GetColumnFamilyMetadataCF returns the metadata of the specified column family.
//
// Note that the caller is responsible to release the returned memory
// using rocksdb_column_family_metadata_destroy.
func (db *DB) GetColumnFamilyMetadataCF(cf *ColumnFamilyHandle) (m *ColumnFamilyMetadata) {
if c := C.rocksdb_get_column_family_metadata_cf(db.c, cf.c); c != nil {
m = newColumnFamilyMetadata(c)
Expand Down
6 changes: 3 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -1511,9 +1511,9 @@ func (opts *Options) SetFIFOCompactionOptions(value *FIFOCompactionOptions) {

// GetStatisticsString returns the statistics as a string.
func (opts *Options) GetStatisticsString() (stats string) {
sString := C.rocksdb_options_statistics_get_string(opts.c)
stats = C.GoString(sString)
C.rocksdb_free(unsafe.Pointer(sString))
cValue := C.rocksdb_options_statistics_get_string(opts.c)
stats = C.GoString(cValue)
C.rocksdb_free(unsafe.Pointer(cValue))
return
}

Expand Down
6 changes: 3 additions & 3 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (transaction *Transaction) SetName(name string) (err error) {

// GetName of transaction.
func (transaction *Transaction) GetName() string {
var nameSize C.size_t
name := C.rocksdb_transaction_get_name(transaction.c, &nameSize)
return C.GoString(name)
var len C.size_t
cValue := C.rocksdb_transaction_get_name(transaction.c, &len)
return toString(cValue, C.int(len))
}

// Prepare transaction.
Expand Down
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ func fromCError(cErr *C.char) (err error) {
}
return
}

func toString(cVal *C.char, len C.int) string {
s := C.GoStringN(cVal, C.int(len))
C.rocksdb_free(unsafe.Pointer(cVal))
return s
}

0 comments on commit b3ad4ec

Please sign in to comment.