PROTON-2199 fix memory leak in C++ object inspect (#244)
diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt
index 80d6d89..2b5730e 100644
--- a/cpp/CMakeLists.txt
+++ b/cpp/CMakeLists.txt
@@ -307,6 +307,9 @@
include_directories(${CMAKE_SOURCE_DIR}/tests/include)
add_executable(cpp-test src/cpp-test.cpp src/url_test.cpp)
target_link_libraries(cpp-test qpid-proton-cpp ${PLATFORM_LIBS})
+# tests that require access to pn_ functions in qpid-proton-core
+add_executable(cpp-core-test src/cpp-test.cpp src/object_test.cpp)
+target_link_libraries(cpp-core-test qpid-proton-cpp qpid-proton-core ${PLATFORM_LIBS})
macro(add_catch_test tag)
pn_add_test(
@@ -314,6 +317,17 @@
NAME cpp-${tag}-test
APPEND_ENVIRONMENT ${test_env}
COMMAND $<TARGET_FILE:cpp-test> "[${tag}]")
+ set_tests_properties(cpp-${tag}-test PROPERTIES FAIL_REGULAR_EXPRESSION ".*No tests ran.*")
endmacro(add_catch_test)
+macro(add_core_catch_test tag)
+ pn_add_test(
+ EXECUTABLE
+ NAME cpp-${tag}-test
+ APPEND_ENVIRONMENT ${test_env}
+ COMMAND $<TARGET_FILE:cpp-core-test> "[${tag}]")
+ set_tests_properties(cpp-${tag}-test PROPERTIES FAIL_REGULAR_EXPRESSION ".*No tests ran.*")
+endmacro(add_core_catch_test)
+
add_catch_test(url)
+add_core_catch_test(object)
diff --git a/cpp/src/object.cpp b/cpp/src/object.cpp
index 2f3a348..2732d5d 100644
--- a/cpp/src/object.cpp
+++ b/cpp/src/object.cpp
@@ -35,6 +35,8 @@
if (!p) return std::string();
::pn_string_t* s = ::pn_string(NULL);
(void) ::pn_inspect(p, s);
- return std::string(pn_string_get(s));
+ std::string tmp = std::string(pn_string_get(s));
+ pn_free(s);
+ return tmp;
}
}}
diff --git a/cpp/src/object_test.cpp b/cpp/src/object_test.cpp
new file mode 100644
index 0000000..4c2737e
--- /dev/null
+++ b/cpp/src/object_test.cpp
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <catch.hpp>
+#include <proton/internal/object.hpp>
+// for pn_data
+#include <proton/codec.h>
+
+namespace {
+
+TEST_CASE("pn_ptr_base(pn_data)", "[object]") {
+ SECTION("incref and decref in constructor") {
+ pn_data_t *o = pn_data(0);
+ CHECK(pn_refcount(o) == 1);
+ {
+ proton::internal::pn_ptr<pn_data_t> ptr = proton::internal::pn_ptr<pn_data_t>(o);
+ CHECK(pn_refcount(o) == 2);
+ }
+ CHECK(pn_refcount(o) == 1);
+ pn_data_free(o);
+ }
+ SECTION("inspect") {
+ pn_data_t *o = pn_data(0);
+ {
+ proton::internal::pn_ptr<pn_data_t> ptr = proton::internal::pn_ptr<pn_data_t>(o);
+ CHECK(ptr.inspect().empty());
+ }
+ pn_data_free(o);
+ }
+}
+
+}// namespace