Browse Source

[2268] Fix a leak upon exception in InMemoryClient's constructor

Mukund Sivaraman 12 years ago
parent
commit
ec2aecacc4

+ 9 - 4
src/lib/datasrc/memory/memory_client.cc

@@ -66,10 +66,15 @@ InMemoryClient::InMemoryClient(util::MemorySegment& mem_sgmt,
                                RRClass rrclass) :
     mem_sgmt_(mem_sgmt),
     rrclass_(rrclass),
-    zone_count_(0),
-    zone_table_(ZoneTable::create(mem_sgmt_, rrclass)),
-    file_name_tree_(FileNameTree::create(mem_sgmt_, false))
-{}
+    zone_count_(0)
+{
+    SegmentObjectHolder<ZoneTable, RRClass> holder(
+        mem_sgmt_, ZoneTable::create(mem_sgmt_, rrclass), rrclass_);
+
+    file_name_tree_ = FileNameTree::create(mem_sgmt_, false);
+
+    zone_table_ = holder.release();
+}
 
 InMemoryClient::~InMemoryClient() {
     FileNameDeleter deleter;

+ 6 - 3
src/lib/datasrc/tests/memory/memory_client_unittest.cc

@@ -261,10 +261,13 @@ TEST_F(MemoryClientTest, loadMemoryAllocationFailures) {
     // Just to check that things get cleaned up
 
     for (int i = 1; i < 16; i++) {
+        SCOPED_TRACE("For throw count = " + i);
         mem_sgmt_.setThrowCount(i);
-        EXPECT_THROW(client_->load(Name("example.org"),
-                                   TEST_DATA_DIR "/example.org.zone"),
-                     std::bad_alloc);
+        EXPECT_THROW({
+            InMemoryClient client2(mem_sgmt_, zclass_);
+            client2.load(Name("example.org"),
+                         TEST_DATA_DIR "/example.org.zone");
+        }, std::bad_alloc);
     }
     // Teardown checks for memory segment leaks
 }