Skip to content

Commit

Permalink
RAMS7200 memory leaks on Tx
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsavulescu committed Nov 14, 2024
1 parent 72b3db4 commit fca45b6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ project(
LANGUAGES CXX
)

set(PROJECT_VERSION 1.0.2)
set(PROJECT_VERSION 1.0.3)

configure_file(config.h.in configured/config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/configured)
Expand Down
9 changes: 7 additions & 2 deletions Common/S7Utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,16 @@ namespace Common{
}

static void TS7AllocateDataItemForAddress(TS7DataItem& item){
Common::S7Utils::TS7DeallocateDataItem(item);
item.pdata = new char[DataSizeByte(item.WordLen )*item.Amount];
std::memset(item.pdata, 0, DataSizeByte(item.WordLen )*item.Amount);
}

static void TS7DeallocateDataItem(TS7DataItem& item){
if(item.pdata != nullptr){
delete[] static_cast<char*>(item.pdata);
item.pdata = nullptr;
}
item.pdata = new char[DataSizeByte(item.WordLen )*item.Amount];
std::memset(item.pdata, 0, DataSizeByte(item.WordLen )*item.Amount);
}

static int GetByteSizeFromAddress(const std::string& Address)
Expand Down
11 changes: 7 additions & 4 deletions RAMS7200LibFacade.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ void RAMS7200LibFacade::RAMS7200ReadWriteMaxN(std::vector<DPInfo> dpItems, std::
} else {
queueAll(std::move(dpItems), std::move(items));
}
} else {
std::for_each(items.begin(), items.end(), [](TS7DataItem& item){
Common::S7Utils::TS7DeallocateDataItem(item);
});
}

}
Expand All @@ -251,8 +255,7 @@ void RAMS7200LibFacade::queueAll(std::vector<DPInfo>&& dpItems, std::vector<TS7D
toDPItems.emplace_back(dpItems[i].dpAddress.c_str(), dpItems[i].dpSize, static_cast<char*>(s7items[i].pdata));
} else {
failed << dpItems[i].dpAddress.c_str() << " ";
delete[] static_cast<char*>(s7items[i].pdata);
s7items[i].pdata = nullptr;
Common::S7Utils::TS7DeallocateDataItem(s7items[i]);
}
}

Expand Down Expand Up @@ -292,12 +295,12 @@ void RAMS7200LibFacade::doSmoothing(std::vector<DPInfo>&& dpItems, std::vector<T
toDPItems.emplace_back(DPInfo.dpAddress.c_str(), dataSize, static_cast<char*>(item.pdata));
Common::Logger::globalInfo(Common::Logger::L4, DPInfo.dpAddress.c_str(), "--> Smoothing updated");
} else {
delete[] static_cast<char*>(item.pdata);
Common::S7Utils::TS7DeallocateDataItem(item);
}
}
} else {
failed << dpItems[i].dpAddress.c_str() << " ";
delete[] static_cast<char*>(item.pdata);
Common::S7Utils::TS7DeallocateDataItem(item);
}
item.pdata = nullptr;
}
Expand Down
23 changes: 13 additions & 10 deletions RAMS7200MS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include <algorithm>


RAMS7200MSVar::RAMS7200MSVar(std::string varName, int pollTime, TS7DataItem type) : varName(varName), pollTime(pollTime), _toPlc(type), _toDP(type){}
RAMS7200MSVar::RAMS7200MSVar(std::string varName, int pollTime, TS7DataItem type) : varName(varName), pollTime(pollTime), _toPlc(type), _toDP(type){
_toPlc.pdata = nullptr;
_toDP.pdata = nullptr;
}

void RAMS7200MS::addVar(std::string varName, int pollTime)
{
Expand All @@ -32,13 +35,9 @@ void RAMS7200MS::removeVar(std::string varName)
std::lock_guard lock{_rwmutex};
auto it = vars.find(varName);
if(it != vars.end()) {
if (it->second._toDP.pdata != nullptr) {
delete[] static_cast<char*>(it->second._toDP.pdata);
}
if (it->second._toPlc.pdata != nullptr) {
delete[] static_cast<char*>(it->second._toPlc.pdata);
}
vars.erase(it);
Common::S7Utils::TS7DeallocateDataItem(it->second._toDP);
Common::S7Utils::TS7DeallocateDataItem(it->second._toPlc);
vars.erase(it);
}
}

Expand All @@ -48,8 +47,12 @@ void RAMS7200MS::queuePLCItem(const std::string& varName, void* item)

if (vars.count(varName) == 0) {
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Undefined variable:", varName.c_str());
delete[] static_cast<char*>(item);
return;
}

vars.at(varName)._toPlc.pdata = item;
auto old_data = std::exchange(vars.at(varName)._toPlc.pdata, item);
if (old_data != nullptr) {
delete[] static_cast<char*>(old_data);
Common::Logger::globalInfo(Common::Logger::L1, "Overwriting old data for:", CharString(_ip.c_str()) + varName.c_str());
}
}
4 changes: 1 addition & 3 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ void PerformTests()
default:
break;
}
if(item.pdata != nullptr){
delete[] item.pdata;
}
Common::S7Utils::TS7DeallocateDataItem(item);
}
}

Expand Down

0 comments on commit fca45b6

Please sign in to comment.