From 4a276b03fa94bd040a3596e77e34ae0468665a10 Mon Sep 17 00:00:00 2001 From: Kai Li Date: Fri, 20 Jun 2014 10:14:19 +0800 Subject: [PATCH] Replace the raw pointers with shared_ptr to ensure memory is released --- tools/extract_features.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tools/extract_features.cpp b/tools/extract_features.cpp index 66d4be1..1bff7ca 100644 --- a/tools/extract_features.cpp +++ b/tools/extract_features.cpp @@ -119,7 +119,7 @@ int feature_extraction_pipeline(int argc, char** argv) { options.error_if_exists = true; options.create_if_missing = true; options.write_buffer_size = 268435456; - vector feature_dbs; + vector > feature_dbs; for (size_t i = 0; i < num_features; ++i) { LOG(INFO)<< "Opening leveldb " << leveldb_names[i]; leveldb::DB* db; @@ -127,7 +127,7 @@ int feature_extraction_pipeline(int argc, char** argv) { leveldb_names[i].c_str(), &db); CHECK(status.ok()) << "Failed to open leveldb " << leveldb_names[i]; - feature_dbs.push_back(db); + feature_dbs.push_back(shared_ptr(db)); } int num_mini_batches = atoi(argv[++arg_pos]); @@ -135,8 +135,9 @@ int feature_extraction_pipeline(int argc, char** argv) { LOG(ERROR)<< "Extacting Features"; Datum datum; - vector feature_batches( - num_features, new leveldb::WriteBatch()); + vector > feature_batches( + num_features, + shared_ptr(new leveldb::WriteBatch())); const int kMaxKeyStrLength = 100; char key_str[kMaxKeyStrLength]; int num_bytes_of_binary_code = sizeof(Dtype); @@ -167,11 +168,11 @@ int feature_extraction_pipeline(int argc, char** argv) { feature_batches[i]->Put(string(key_str), value); ++image_indices[i]; if (image_indices[i] % 1000 == 0) { - feature_dbs[i]->Write(leveldb::WriteOptions(), feature_batches[i]); + feature_dbs[i]->Write(leveldb::WriteOptions(), + feature_batches[i].get()); LOG(ERROR)<< "Extracted features of " << image_indices[i] << " query images for feature blob " << blob_names[i]; - delete feature_batches[i]; - feature_batches[i] = new leveldb::WriteBatch(); + feature_batches[i].reset(new leveldb::WriteBatch()); } } // for (int n = 0; n < batch_size; ++n) } // for (int i = 0; i < num_features; ++i) @@ -179,7 +180,7 @@ int feature_extraction_pipeline(int argc, char** argv) { // write the last batch for (int i = 0; i < num_features; ++i) { if (image_indices[i] % 1000 != 0) { - feature_dbs[i]->Write(leveldb::WriteOptions(), feature_batches[i]); + feature_dbs[i]->Write(leveldb::WriteOptions(), feature_batches[i].get()); } LOG(ERROR)<< "Extracted features of " << image_indices[i] << " query images for feature blob " << blob_names[i]; -- 2.7.4