]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
tools/rados: fix memory leak in error path
authorLi Wang <laurence.liwang@gmail.com>
Thu, 6 Dec 2018 08:38:03 +0000 (08:38 +0000)
committerLi Wang <laurence.liwang@gmail.com>
Thu, 6 Dec 2018 08:38:03 +0000 (08:38 +0000)
Signed-off-by: Li Wang <laurence.liwang@gmail.com>
src/common/obj_bencher.cc

index 9b0bb79fc308f2280157e64a9b69cda096eb55d9..2470cc80c62bafca2dc7184d94029015ef250cda 100644 (file)
@@ -409,7 +409,7 @@ int ObjBencher::write_bench(int secondsToRun,
 
   std::vector<string> name(concurrentios);
   std::string newName;
-  bufferlist* contents[concurrentios];
+  unique_ptr<bufferlist> contents[concurrentios];
   int r = 0;
   bufferlist b_write;
   lock_cond lc(&lock);
@@ -427,7 +427,7 @@ int ObjBencher::write_bench(int secondsToRun,
   //set up writes so I can start them together
   for (int i = 0; i<concurrentios; ++i) {
     name[i] = generate_object_name_fast(i / writes_per_object);
-    contents[i] = new bufferlist();
+    contents[i] = std::make_unique<bufferlist>();
     snprintf(data.object_contents, data.op_size, "I'm the %16dth op!", i);
     contents[i]->append(data.object_contents, data.op_size);
   }
@@ -486,7 +486,7 @@ int ObjBencher::write_bench(int secondsToRun,
     lock.unlock();
     //create new contents and name on the heap, and fill them
     newName = generate_object_name_fast(data.started / writes_per_object);
-    newContents = contents[slot];
+    newContents = contents[slot].get();
     snprintf(newContents->c_str(), data.op_size, "I'm the %16dth op!", data.started);
     // we wrote to buffer, going around internal crc cache, so invalidate it now.
     newContents->invalidate_crc();
@@ -557,8 +557,6 @@ int ObjBencher::write_bench(int secondsToRun,
     --data.in_flight;
     lock.unlock();
     release_completion(slot);
-    delete contents[slot];
-    contents[slot] = 0;
   }
 
   timePassed = mono_clock::now() - data.start_time;
@@ -638,9 +636,6 @@ int ObjBencher::write_bench(int secondsToRun,
   sync_write(run_name_meta, b_write, sizeof(int)*3);
 
   completions_done();
-  for (int i = 0; i < concurrentios; i++)
-      if (contents[i])
-          delete contents[i];
 
   return 0;
 
@@ -649,9 +644,6 @@ int ObjBencher::write_bench(int secondsToRun,
   data.done = 1;
   lock.unlock();
   pthread_join(print_thread, NULL);
-  for (int i = 0; i < concurrentios; i++)
-      if (contents[i])
-          delete contents[i];
   return r;
 }
 
@@ -663,7 +655,7 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
 
   std::vector<string> name(concurrentios);
   std::string newName;
-  bufferlist* contents[concurrentios];
+  unique_ptr<bufferlist> contents[concurrentios];
   int index[concurrentios];
   int errors = 0;
   double total_latency = 0;
@@ -685,7 +677,7 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
   //set up initial reads
   for (int i = 0; i < concurrentios; ++i) {
     name[i] = generate_object_name_fast(i / writes_per_object, pid);
-    contents[i] = new bufferlist();
+    contents[i] = std::make_unique<bufferlist>();
   }
 
   lock.lock();
@@ -703,7 +695,7 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
     index[i] = i;
     start_times[i] = mono_clock::now();
     create_completion(i, _aio_cb, (void *)&lc);
-    r = aio_read(name[i], i, contents[i], data.op_size,
+    r = aio_read(name[i], i, contents[i].get(), data.op_size,
                 data.op_size * (i % writes_per_object));
     if (r < 0) { //naughty, doesn't clean up heap -- oh, or handle the print thread!
       cerr << "r = " << r << std::endl;
@@ -745,7 +737,7 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
     // calculate latency here, so memcmp doesn't inflate it
     data.cur_latency = mono_clock::now() - start_times[slot];
 
-    cur_contents = contents[slot];
+    cur_contents = contents[slot].get();
     int current_index = index[slot];
     
     // invalidate internal crc cache
@@ -785,7 +777,7 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
     //start new read and check data if requested
     start_times[slot] = mono_clock::now();
     create_completion(slot, _aio_cb, (void *)&lc);
-    r = aio_read(newName, slot, contents[slot], data.op_size,
+    r = aio_read(newName, slot, contents[slot].get(), data.op_size,
                 data.op_size * (data.started % writes_per_object));
     if (r < 0) {
       goto ERR;
@@ -829,7 +821,6 @@ int ObjBencher::seq_read_bench(int seconds_to_run, int num_objects, int concurre
     } else {
         lock.unlock();
     }
-    delete contents[slot];
   }
 
   timePassed = mono_clock::now() - data.start_time;
@@ -899,7 +890,7 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
 
   std::vector<string> name(concurrentios);
   std::string newName;
-  bufferlist* contents[concurrentios];
+  unique_ptr<bufferlist> contents[concurrentios];
   int index[concurrentios];
   int errors = 0;
   int r = 0;
@@ -923,7 +914,7 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
   //set up initial reads
   for (int i = 0; i < concurrentios; ++i) {
     name[i] = generate_object_name_fast(i / writes_per_object, pid);
-    contents[i] = new bufferlist();
+    contents[i] = std::make_unique<bufferlist>();
   }
 
   lock.lock();
@@ -941,7 +932,7 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
     index[i] = i;
     start_times[i] = mono_clock::now();
     create_completion(i, _aio_cb, (void *)&lc);
-    r = aio_read(name[i], i, contents[i], data.op_size,
+    r = aio_read(name[i], i, contents[i].get(), data.op_size,
                 data.op_size * (i % writes_per_object));
     if (r < 0) { //naughty, doesn't clean up heap -- oh, or handle the print thread!
       cerr << "r = " << r << std::endl;
@@ -986,7 +977,7 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
     lock.unlock();
 
     int current_index = index[slot];
-    cur_contents = contents[slot];
+    cur_contents = contents[slot].get();
     completion_wait(slot);
     lock.lock();
     r = completion_ret(slot);
@@ -1026,7 +1017,7 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
     //start new read and check data if requested
     start_times[slot] = mono_clock::now();
     create_completion(slot, _aio_cb, (void *)&lc);
-    r = aio_read(newName, slot, contents[slot], data.op_size,
+    r = aio_read(newName, slot, contents[slot].get(), data.op_size,
                 data.op_size * (rand_id % writes_per_object));
     if (r < 0) {
       goto ERR;
@@ -1071,7 +1062,6 @@ int ObjBencher::rand_read_bench(int seconds_to_run, int num_objects, int concurr
     } else {
         lock.unlock();
     }
-    delete contents[slot];
   }
 
   timePassed = mono_clock::now() - data.start_time;