Skip to content

Commit

Permalink
Fix a memory leak in FGSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni committed Feb 9, 2025
1 parent 0ac6194 commit 9a7cc8b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/models/flight_control/FGSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ CLASS IMPLEMENTATION
FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
{
string value;
Test *current_test;
unique_ptr<Test> current_test;
auto PropertyManager = fcs->GetPropertyManager();

bind(element, PropertyManager.get()); // Bind() this component here in case it is used in its own
// definition for a sample-and-hold
Element* test_element = element->FindElement("default");
if (test_element) {
try {
current_test = new Test;
current_test = make_unique<Test>();
value = test_element->GetAttributeValue("value");
current_test->setTestValue(value, Name, PropertyManager, test_element);
current_test->Default = true;
Expand All @@ -95,7 +95,7 @@ FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
output_array[i] = v; // for the switch if that value is a number.
}
}
tests.push_back(current_test);
tests.push_back(current_test.release());
} catch (const BaseException& e) {
FGXMLLogging log(fcs->GetExec()->GetLogger(), test_element, LogLevel::ERROR);
log << e.what() << "\n"
Expand All @@ -106,11 +106,11 @@ FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
test_element = element->FindElement("test");
while (test_element) {
try {
current_test = new Test;
current_test->condition = new FGCondition(test_element, PropertyManager);
current_test = make_unique<Test>();
current_test->condition = make_unique<FGCondition>(test_element, PropertyManager);
value = test_element->GetAttributeValue("value");
current_test->setTestValue(value, Name, PropertyManager, test_element);
tests.push_back(current_test);
tests.push_back(current_test.release());
} catch (const BaseException& e) {
FGXMLLogging log(fcs->GetExec()->GetLogger(), test_element, LogLevel::ERROR);
log << e.what() << "\n"
Expand All @@ -127,11 +127,7 @@ FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)

FGSwitch::~FGSwitch()
{
for (auto test: tests) {
delete test->condition;
delete test;
}

for (auto test: tests) delete test;
Debug(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/flight_control/FGSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class FGSwitch : public FGFCSComponent
private:

struct Test {
FGCondition* condition;
std::unique_ptr<FGCondition> condition;
bool Default;
FGParameter_ptr OutputValue;

Expand Down

0 comments on commit 9a7cc8b

Please sign in to comment.