Skip to content

Commit

Permalink
Memory leak fix #398
Browse files Browse the repository at this point in the history
  • Loading branch information
searchivarius committed Jun 19, 2019
1 parent d133e36 commit c817f21
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions python_bindings/nmslib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ template <typename dist_t> void exportIndex(py::module * m);
template <typename dist_t> std::string distName();
AnyParams loadParams(py::object o);
void exportLegacyAPI(py::module * m);
void freeObjectVector(ObjectVector * data);
void freeAndClearObjectVector(ObjectVector& data);

// Wrap a space/objectvector/index together for ease of use
template <typename dist_t>
Expand Down Expand Up @@ -101,7 +101,7 @@ struct IndexWrapper {
index.reset(factory.CreateMethod(print_progress, method, space_type, *space, data));
if (load_data) {
vector<string> dummy;
data.clear();
freeAndClearObjectVector(data);
space->ReadObjectVectorFromBinData(data, dummy, filename + data_suff);
}
index->LoadIndex(filename);
Expand Down Expand Up @@ -156,7 +156,7 @@ struct IndexWrapper {
});

// TODO(@benfred): some sort of RAII auto-destroy for this
freeObjectVector(&queries);
freeAndClearObjectVector(queries);
}

py::list ret;
Expand Down Expand Up @@ -362,7 +362,7 @@ struct IndexWrapper {
// In cases when the interpreter was shutting down, attempting to log in python
// could throw an exception (https://github.com/nmslib/nmslib/issues/327).
//LOG(LIB_DEBUG) << "Destroying Index";
freeObjectVector(&data);
freeAndClearObjectVector(data);
}

std::string method;
Expand Down Expand Up @@ -657,10 +657,11 @@ template <> std::string distName<int>() { return "Int"; }
template <> std::string distName<float>() { return "Float"; }
template <> std::string distName<double>() { return "Double"; }

void freeObjectVector(ObjectVector * data) {
for (auto datum : *data) {
void freeAndClearObjectVector(ObjectVector& data) {
for (auto datum : data) {
delete datum;
}
data.clear();
}

AnyParams loadParams(py::object o) {
Expand Down
2 changes: 1 addition & 1 deletion python_bindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import setuptools

__version__ = '1.8'
__version__ = '1.8.1'

libdir = os.path.join(".", "nmslib", "similarity_search")
if not os.path.isdir(libdir) and sys.platform.startswith("win"):
Expand Down

0 comments on commit c817f21

Please sign in to comment.