From 21ad4bda778e28b98392bb73c923beaea5295a84 Mon Sep 17 00:00:00 2001 From: Pierre Willenbrock Date: Mon, 9 Jan 2023 17:26:10 +0100 Subject: [PATCH 1/4] fix: Fix some memory leaks --- src/ConfigurationHelper.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ConfigurationHelper.cpp b/src/ConfigurationHelper.cpp index 8cccec1..26a26b5 100644 --- a/src/ConfigurationHelper.cpp +++ b/src/ConfigurationHelper.cpp @@ -318,9 +318,9 @@ bool ConfigurationHelper::applyConfOnTyplibValue(Typelib::Value &value, const Co for(const std::shared_ptr val: array->getValues()) { - - //TODO check, this may be a memory leak - Typelib::Value v(new uint8_t[indirect.getSize()], indirect); + std::vector storage; + storage.resize(indirect.getSize()); + Typelib::Value v(storage.data(), indirect); Typelib::init(v); Typelib::zero(v); @@ -330,6 +330,8 @@ bool ConfigurationHelper::applyConfOnTyplibValue(Typelib::Value &value, const Co } cont->push(value.getData(), v); + + Typelib::destroy(v); } } } @@ -406,8 +408,14 @@ bool ConfigurationHelper::applyConfigValueOnDSB(RTT::base::DataSourceBase::share typelibTransport->refreshTypelibSample(handle); } - if(!applyConfOnTyplibValue(dest, value)) + if(!applyConfOnTyplibValue(dest, value)) { + //destroy handle to avoid memory leak + //this also deletes all referenced memory by calling + //orogen_transports::TypelibMarshaller::deleteSamples + typelibTransport->deleteHandle(handle); + return false; + } //we modified the typlib samples, so we need to trigger the opaque @@ -416,10 +424,12 @@ bool ConfigurationHelper::applyConfigValueOnDSB(RTT::base::DataSourceBase::share //write value back typelibTransport->writeDataSource(*dsb, handle); - + //destroy handle to avoid memory leak + //this also deletes all referenced memory by calling + //orogen_transports::TypelibMarshaller::deleteSamples typelibTransport->deleteHandle(handle); - + return true; } From fa981ca5db1761a07a3f4e9d7bd1c793c683817f Mon Sep 17 00:00:00 2001 From: Pierre Willenbrock Date: Thu, 15 Jan 2026 16:56:46 +0100 Subject: [PATCH 2/4] fix: Special case bool and (u)int8_t when writing config strings --- src/ConfigurationHelper.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ConfigurationHelper.cpp b/src/ConfigurationHelper.cpp index 26a26b5..3eb7969 100644 --- a/src/ConfigurationHelper.cpp +++ b/src/ConfigurationHelper.cpp @@ -564,7 +564,12 @@ bool ConfigurationHelper::registerOverride(const std::string& taskName, Configur } YAML::Emitter &toYAML(YAML::Emitter &out, const Typelib::Numeric &type, const Typelib::Value &value){ - + if (type.getName() == "/bool") + { + //the /bool type must be output as "true" or "false" + out << (*static_cast(value.getData())?"true":"false"); + return out; + } switch(type.getNumericCategory()) { case Typelib::Numeric::Float: @@ -581,7 +586,8 @@ YAML::Emitter &toYAML(YAML::Emitter &out, const Typelib::Numeric &type, const Ty switch(type.getSize()) { case sizeof(int8_t): - out << *(static_cast(value.getData())); + //need to cast uint8_t to int so it is not interpreted as character but as number + out << static_cast(*(static_cast(value.getData()))); break; case sizeof(int16_t): out << *(static_cast(value.getData())); @@ -603,7 +609,8 @@ YAML::Emitter &toYAML(YAML::Emitter &out, const Typelib::Numeric &type, const Ty switch(type.getSize()) { case sizeof(uint8_t): - out << *(static_cast(value.getData())); + //need to cast uint8_t to unsigned int so it is not interpreted as character but as number + out << static_cast(*(static_cast(value.getData()))); break; case sizeof(uint16_t): out << *(static_cast(value.getData())); From 7c9b7308ae7fc924ed051461603918421fabac94 Mon Sep 17 00:00:00 2001 From: Pierre Willenbrock Date: Thu, 15 Jan 2026 17:11:37 +0100 Subject: [PATCH 3/4] fix: No need to cast (u)int8_t to (u)int when finally casting to double --- src/TypeWrapper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TypeWrapper.cpp b/src/TypeWrapper.cpp index ed89242..a57c0af 100644 --- a/src/TypeWrapper.cpp +++ b/src/TypeWrapper.cpp @@ -93,7 +93,7 @@ double TypeWrapper::toDouble() { } case Typelib::Numeric::SInt: switch (numeric.getSize()) { - case sizeof(int8_t): return static_cast(*static_cast(value.getData())); + case sizeof(int8_t): return *static_cast(value.getData()); case sizeof(int16_t): return *static_cast(value.getData()); case sizeof(int32_t): return *static_cast(value.getData()); case sizeof(int64_t): return *static_cast(value.getData()); @@ -103,7 +103,7 @@ double TypeWrapper::toDouble() { } case Typelib::Numeric::UInt: { switch (numeric.getSize()) { - case sizeof(uint8_t): return static_cast(*static_cast(value.getData())); + case sizeof(uint8_t): return *static_cast(value.getData()); case sizeof(uint16_t): return *static_cast(value.getData()); case sizeof(uint32_t): return *static_cast(value.getData()); case sizeof(uint64_t): return *static_cast(value.getData()); From 374d135a49c003a28c5b72c736fac8e2278f57f9 Mon Sep 17 00:00:00 2001 From: Pierre Willenbrock Date: Fri, 16 Jan 2026 17:12:17 +0100 Subject: [PATCH 4/4] fix: Convert numbers to strings to concatenating with operator+ instead of indexing into the const char * literal --- src/ConfigurationHelper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfigurationHelper.cpp b/src/ConfigurationHelper.cpp index 3eb7969..2b8fccf 100644 --- a/src/ConfigurationHelper.cpp +++ b/src/ConfigurationHelper.cpp @@ -600,7 +600,7 @@ YAML::Emitter &toYAML(YAML::Emitter &out, const Typelib::Numeric &type, const Ty break; default: std::cerr << "Error, got integer of unexpected size " << type.getSize() << std::endl; - throw std::runtime_error("got integer of unexpected size " + type.getSize()); + throw std::runtime_error("got integer of unexpected size " + std::to_string(type.getSize())); break; } break; @@ -623,7 +623,7 @@ YAML::Emitter &toYAML(YAML::Emitter &out, const Typelib::Numeric &type, const Ty break; default: std::cout << "Error, got integer of unexpected size " << type.getSize() << std::endl; - throw std::runtime_error("got integer of unexpected size " + type.getSize()); + throw std::runtime_error("got integer of unexpected size " + std::to_string(type.getSize())); break; } }