Skip to content

Commit ffece5f

Browse files
committed
test/mon/test_config_map: free CephContext created with new
before this change, we create a new cct instance with `new`, but we never free this instance after done with it. and LeakSanitizer points this out: ``` Indirect leak of 10880000 byte(s) in 1 object(s) allocated from: #0 0x561afe148fed in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_config_map+0x1c2fed) (BuildId: 3ce9eeed38cee335628fa74fdd08cd215b15019e) ceph#1 0x7f37dc9ac189 in __gnu_cxx::new_allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27 ceph#2 0x7f37dc9ab563 in std::allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 ceph#3 0x7f37dc9ab563 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::allocate(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2396:39 ceph#4 0x7f37dc9ab2c0 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::initialize_buffer(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2494:18 ceph#5 0x7f37dc9a5192 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::circular_buffer(unsigned long, std::allocator<ceph::logging::ConcreteEntry> const&) /opt/ceph/include/boost/circular_buffer/base.hpp:1039:9 ceph#6 0x7f37dc9981e4 in ceph::logging::Log::Log(ceph::logging::SubsystemMap const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/log/Log.cc:53:5 ceph#7 0x7f37dbc6e96d in ceph::common::CephContext::CephContext(unsigned int, ceph::common::CephContext::create_options const&) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:729:16 ceph#8 0x7f37dbc6d93b in ceph::common::CephContext::CephContext(unsigned int, code_environment_t, int) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:697:5 ceph#9 0x561afe14e983 in ConfigMap_add_option_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/mon/test_config_map.cc:58:18 ceph#10 0x561afe2689b6 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10 ceph#11 0x561afe221262 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14 ceph#12 0x561afe1d1f7c in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5 ceph#13 0x561afe1d3fb2 in testing::TestInfo::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2858:11 ceph#14 0x561afe1d55eb in testing::TestSuite::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3012:28 ceph#15 0x561afe1f2a78 in testing::internal::UnitTestImpl::RunAllTests() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5723:44 ceph#16 0x561afe2711e6 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10 ceph#17 0x561afe227bd2 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14 ceph#18 0x561afe1f1e02 in testing::UnitTest::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:5306:10 ceph#19 0x561afe176ec0 in RUN_ALL_TESTS() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:2486:46 ceph#20 0x561afe176e51 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googlemock/src/gmock_main.cc:70:10 ceph#21 0x7f37d9397d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16 ``` so in this change, we manage the `CephContext` pointer with a smart pointer. because the size of CephContext could be large, we don't create it on stack. Signed-off-by: Kefu Chai <[email protected]>
1 parent 742d5fa commit ffece5f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/test/mon/test_config_map.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ TEST(ConfigMap, parse_key)
5555
TEST(ConfigMap, add_option)
5656
{
5757
ConfigMap cm;
58-
auto cct = new CephContext(CEPH_ENTITY_TYPE_MON);
58+
boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
5959
int r;
6060

6161
r = cm.add_option(
62-
cct, "foo", "global", "fooval",
62+
cct.get(), "foo", "global", "fooval",
6363
[&](const std::string& name) {
6464
return nullptr;
6565
});
6666
ASSERT_EQ(0, r);
6767
ASSERT_EQ(1, cm.global.options.size());
6868

6969
r = cm.add_option(
70-
cct, "foo", "mon", "fooval",
70+
cct.get(), "foo", "mon", "fooval",
7171
[&](const std::string& name) {
7272
return nullptr;
7373
});
@@ -76,7 +76,7 @@ TEST(ConfigMap, add_option)
7676
ASSERT_EQ(1, cm.by_type["mon"].options.size());
7777

7878
r = cm.add_option(
79-
cct, "foo", "mon.a", "fooval",
79+
cct.get(), "foo", "mon.a", "fooval",
8080
[&](const std::string& name) {
8181
return nullptr;
8282
});
@@ -89,22 +89,22 @@ TEST(ConfigMap, add_option)
8989
TEST(ConfigMap, result_sections)
9090
{
9191
ConfigMap cm;
92-
auto cct = new CephContext(CEPH_ENTITY_TYPE_MON);
92+
boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
9393
auto crush = new CrushWrapper;
9494
crush->finalize();
9595

9696
int r;
9797

9898
r = cm.add_option(
99-
cct, "foo", "global", "g",
99+
cct.get(), "foo", "global", "g",
100100
[&](const std::string& name) {
101101
return nullptr;
102102
});
103103
ASSERT_EQ(0, r);
104104
ASSERT_EQ(1, cm.global.options.size());
105105

106106
r = cm.add_option(
107-
cct, "foo", "mon", "m",
107+
cct.get(), "foo", "mon", "m",
108108
[&](const std::string& name) {
109109
return nullptr;
110110
});
@@ -113,7 +113,7 @@ TEST(ConfigMap, result_sections)
113113
ASSERT_EQ(1, cm.by_type["mon"].options.size());
114114

115115
r = cm.add_option(
116-
cct, "foo", "mon.a", "a",
116+
cct.get(), "foo", "mon.a", "a",
117117
[&](const std::string& name) {
118118
return nullptr;
119119
});

0 commit comments

Comments
 (0)