psusensor: Fix memory leak
Remove the psuProperties vector since older params are not being used.
Use a temp copy of the psuProperty for customization per label match.
This change fix the memory leak when createSensors is called
repeatedly.
Tested:
Memory stay stable with overnight cold reset test.
Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: If4ec633fd533fdb90c7fc9f655e82bef2616e3f3
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index 35610c5..93d280f 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -138,7 +138,6 @@
static GroupEventPathList groupEventMatch;
static EventPathList limitEventMatch;
-static std::vector<PSUProperty> psuProperties;
static boost::container::flat_map<size_t, bool> cpuPresence;
static boost::container::flat_map<DevTypes, DevParams> devParamMap;
@@ -668,8 +667,7 @@
// by making a copy and modifying that instead.
// Avoid bleedthrough of one device's customizations to
// the next device, as each should be independently customizable.
- psuProperties.push_back(findProperty->second);
- auto psuProperty = psuProperties.rbegin();
+ PSUProperty psuProperty = findProperty->second;
// Use label head as prefix for reading from config file,
// example if temp1: temp1_Name, temp1_Scale, temp1_Min, ...
@@ -686,7 +684,7 @@
{
try
{
- psuProperty->labelTypeName = std::visit(
+ psuProperty.labelTypeName = std::visit(
VariantToStringVisitor(), findCustomName->second);
}
catch (const std::invalid_argument&)
@@ -705,7 +703,7 @@
{
try
{
- psuProperty->sensorScaleFactor = std::visit(
+ psuProperty.sensorScaleFactor = std::visit(
VariantToUnsignedIntVisitor(), findCustomScale->second);
}
catch (const std::invalid_argument&)
@@ -715,7 +713,7 @@
}
// Avoid later division by zero
- if (psuProperty->sensorScaleFactor > 0)
+ if (psuProperty.sensorScaleFactor > 0)
{
customizedScale = true;
}
@@ -731,7 +729,7 @@
{
try
{
- psuProperty->minReading = std::visit(
+ psuProperty.minReading = std::visit(
VariantToDoubleVisitor(), findCustomMin->second);
}
catch (const std::invalid_argument&)
@@ -746,7 +744,7 @@
{
try
{
- psuProperty->maxReading = std::visit(
+ psuProperty.maxReading = std::visit(
VariantToDoubleVisitor(), findCustomMax->second);
}
catch (const std::invalid_argument&)
@@ -761,7 +759,7 @@
{
try
{
- psuProperty->sensorOffset = std::visit(
+ psuProperty.sensorOffset = std::visit(
VariantToDoubleVisitor(), findCustomOffset->second);
}
catch (const std::invalid_argument&)
@@ -779,7 +777,7 @@
findPowerState->second);
setReadState(powerState, readState);
}
- if (!(psuProperty->minReading < psuProperty->maxReading))
+ if (!(psuProperty.minReading < psuProperty.maxReading))
{
std::cerr << "Min must be less than Max\n";
continue;
@@ -837,7 +835,7 @@
// Similarly, if sensor scaling factor is being customized,
// then the below power-of-10 constraint becomes unnecessary,
// as config should be able to specify an arbitrary divisor.
- unsigned int factor = psuProperty->sensorScaleFactor;
+ unsigned int factor = psuProperty.sensorScaleFactor;
if (!customizedScale)
{
// Preserve existing usage of hardcoded labelMatch table below
@@ -884,14 +882,14 @@
if constexpr (debug)
{
std::cerr << "Sensor properties: Name \""
- << psuProperty->labelTypeName << "\" Scale "
- << psuProperty->sensorScaleFactor << " Min "
- << psuProperty->minReading << " Max "
- << psuProperty->maxReading << " Offset "
- << psuProperty->sensorOffset << "\n";
+ << psuProperty.labelTypeName << "\" Scale "
+ << psuProperty.sensorScaleFactor << " Min "
+ << psuProperty.minReading << " Max "
+ << psuProperty.maxReading << " Offset "
+ << psuProperty.sensorOffset << "\n";
}
- std::string sensorName = psuProperty->labelTypeName;
+ std::string sensorName = psuProperty.labelTypeName;
if (customizedName)
{
if (sensorName.empty())
@@ -906,8 +904,7 @@
{
// Sensor name not customized, do prefix/suffix composition,
// preserving default behavior by using psuNameFromIndex.
- sensorName = psuNameFromIndex + " " +
- psuProperty->labelTypeName;
+ sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
}
if constexpr (debug)
@@ -934,8 +931,8 @@
sensorPathStr, sensorType, objectServer, dbusConnection, io,
sensorName, std::move(sensorThresholds), *interfacePath,
readState, findSensorUnit->second, factor,
- psuProperty->maxReading, psuProperty->minReading,
- psuProperty->sensorOffset, labelHead, thresholdConfSize,
+ psuProperty.maxReading, psuProperty.minReading,
+ psuProperty.sensorOffset, labelHead, thresholdConfSize,
pollRate, i2cDev);
sensors[sensorName]->setupRead();
++numCreated;