From 4844c617de86233bb8f438f2d368f463277a6768 Mon Sep 17 00:00:00 2001 From: Jay Chiu Date: Tue, 6 Dec 2016 23:25:34 -0800 Subject: [PATCH] Fix some cgo memory leaks --- raftwal/wal.go | 2 ++ rdb/db.go | 3 ++- worker/predicate.go | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/raftwal/wal.go b/raftwal/wal.go index 61cfffc944f..21a7483c7d3 100644 --- a/raftwal/wal.go +++ b/raftwal/wal.go @@ -89,6 +89,7 @@ func (w *Wal) Store(gid uint32, s raftpb.Snapshot, h raftpb.HardState, es []raft start := w.entryKey(gid, t, i+1) prefix := w.prefix(gid) itr := w.wals.NewIterator() + defer itr.Close() for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() { b.Delete(itr.Key().Data()) @@ -123,6 +124,7 @@ func (w *Wal) Entries(gid uint32, fromTerm, fromIndex uint64) (es []raftpb.Entry start := w.entryKey(gid, fromTerm, fromIndex) prefix := w.prefix(gid) itr := w.wals.NewIterator() + defer itr.Close() for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() { data := itr.Value().Data() diff --git a/rdb/db.go b/rdb/db.go index 62748319924..08c994e3d12 100644 --- a/rdb/db.go +++ b/rdb/db.go @@ -66,7 +66,8 @@ func (db *DB) Close() { C.rdb_close(db.c) } -// Get returns the data associated with the key from the database. +// Get returns the data associated with the key from the database. Remember +// to deallocate the returned Slice. func (db *DB) Get(opts *ReadOptions, key []byte) (*Slice, error) { var ( cErr *C.char diff --git a/worker/predicate.go b/worker/predicate.go index 5124ffba19d..35d3f52ed0e 100644 --- a/worker/predicate.go +++ b/worker/predicate.go @@ -36,6 +36,7 @@ const ( // writeBatch performs a batch write of key value pairs to RocksDB. func writeBatch(ctx context.Context, kv chan *task.KV, che chan error) { wb := pstore.NewWriteBatch() + defer wb.Destroy() batchSize := 0 batchWriteNum := 1 for i := range kv {