Skip to content

Commit

Permalink
Merge pull request #1238 from sys-bio/fixing_the_issue_with_memory_le…
Browse files Browse the repository at this point in the history
…ak_of_auto2000_plugin

fix: resolve memory leaks in auto2000 plugin
  • Loading branch information
luciansmith authored Oct 24, 2024
2 parents 712ce0b + 44a0938 commit 26dc2b4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,28 @@ void AutoTellurimInterface::setInitialPCPValue()
if(mAutoConstants.PreSimulation)
{
// Two simulations gives better results.
gHostInterface->simulateEx(mRR, mAutoConstants.PreSimulationStart, mAutoConstants.PreSimulationDuration, mAutoConstants.PreSimulationSteps);
gHostInterface->simulateEx(mRR, mAutoConstants.PreSimulationStart, mAutoConstants.PreSimulationDuration, mAutoConstants.PreSimulationSteps);
rrc::RRCDataPtr simulationData = gHostInterface->simulateEx(mRR, mAutoConstants.PreSimulationStart, mAutoConstants.PreSimulationDuration, mAutoConstants.PreSimulationSteps);
if (simulationData != NULL) {
delete[] simulationData->Weights;
if (simulationData->ColumnHeaders != NULL) {
for (int i = 0; i < simulationData->CSize; i++)
delete[] simulationData->ColumnHeaders[i];
delete[] simulationData->ColumnHeaders;
}
delete[] simulationData->Data;
delete simulationData;
}
simulationData = gHostInterface->simulateEx(mRR, mAutoConstants.PreSimulationStart, mAutoConstants.PreSimulationDuration, mAutoConstants.PreSimulationSteps);
if (simulationData != NULL) {
delete[] simulationData->Weights;
if (simulationData->ColumnHeaders != NULL) {
for (int i = 0; i < simulationData->CSize; i++)
delete[] simulationData->ColumnHeaders[i];
delete[] simulationData->ColumnHeaders;
}
delete[] simulationData->Data;
delete simulationData;
}
}

double* val= new double; //just to make the call compatible else no use
Expand Down Expand Up @@ -147,12 +167,22 @@ bool AutoTellurimInterface::setupUsingCurrentModel()
rrc::RRStringArrayPtr lists= gHostInterface->getGlobalParameterIds(mRR);
StringList list1(lists->String,lists->Count);
mModelParameters = list1;
for (int i = 0; i < lists->Count; i++) {
delete[] lists->String[i];
}
delete[] lists->String;
delete lists;

lists= gHostInterface->getBoundarySpeciesIds(mRR);
if (lists) {
StringList list2(lists->String, lists->Count);
//Boundary species can be used as PCP as well
mModelBoundarySpecies = list2;
for (int i = 0; i < lists->Count; i++) {
delete[] lists->String[i];
}
delete[] lists->String;
delete lists;
}
//Set initial value of Primary continuation parameter
setInitialPCPValue();
Expand Down Expand Up @@ -223,17 +253,24 @@ int autoCallConv AutoTellurimInterface::ModelInitializationCallback(long ndim, d
}

int nrIndFloatingSpecies = gHostInterface->_getNumIndFloatingSpecies(mRR)+gHostInterface->_getNumRateRules(mRR);
double* floatCon = new double[nrIndFloatingSpecies];

floatCon = (gHostInterface->getFloatingSpeciesConcentrations(mRR))->Data;
rrc::RRVectorPtr floatCon = (gHostInterface->getFloatingSpeciesConcentrations(mRR));

int nMin = min(nrIndFloatingSpecies, ndim);
for(int i = 0; i < nMin; i++)
{
u[i] = floatCon[i];
u[i] = floatCon->Data[i];
}

delete[] floatCon;
if (floatCon != NULL) {
if (floatCon->Data != NULL) {
delete[] floatCon->Data;
floatCon->Data = NULL;
}

delete floatCon;
floatCon = NULL;
}

return 0;
}

Expand Down Expand Up @@ -284,6 +321,13 @@ void autoCallConv AutoTellurimInterface::ModelFunctionCallback(const double* oVa

rrc::RRStringArrayPtr temp= gHostInterface->getSteadyStateSelectionList(mRR);
tlp::StringList selRecs(temp->String, temp->Count);
if (temp->String) {
for (int i = 0; i < temp->Count; i++) {
delete[] temp->String[i];
}
delete[] temp->String;
}
delete temp;

tlp::StringList selList = selRecs;
vector<double> variableTemp(selList.size());
Expand Down Expand Up @@ -314,10 +358,10 @@ void autoCallConv AutoTellurimInterface::ModelFunctionCallback(const double* oVa
}
}

rrc::RRVectorPtr intermediate= new rrc::RRVector;
std::unique_ptr<rrc::RRVector> intermediate = std::make_unique<rrc::RRVector>();
intermediate->Count = numIndFloatingSpecies;
intermediate->Data = tempConc;
gHostInterface->setFloatingSpeciesConcentrations(mRR, intermediate);
gHostInterface->setFloatingSpeciesConcentrations(mRR, intermediate.get());


delete [] tempConc;
Expand Down
9 changes: 9 additions & 0 deletions rrplugins/plugins/auto2000/telAutoWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ void AutoWorker::run()
rrc::RRStringArrayPtr temp = gHostInterface->getSteadyStateSelectionList(mTheHost.rrHandle);
StringList selRecs (temp->String, temp->Count);
StringList selList = selRecs;
if (temp != NULL) {
if (temp->String != NULL) {
for (int i = 0; i < temp->Count; i++) {
delete[] temp->String[i];
}
delete[] temp->String;
}
delete temp;
}

TelluriumData& data = mTheHost.mBifurcationData.getValueReference();

Expand Down
6 changes: 4 additions & 2 deletions rrplugins/plugins/levenberg_marquardt/lm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ namespace lmfit
mProperties.add(&mStatusMessage);

//Allocate model and Residuals data
mResidualsData.setValue(new TelluriumData());
mModelData.setValue(new TelluriumData());
TelluriumData residualsData;
mResidualsData.setValue(&residualsData);
TelluriumData modelData;
mModelData.setValue(&modelData);

mHint = "Parameter fitting using the Levenberg-Marquardt algorithm";
mDescription = "The Levenberg-Marquardt plugin is used to fit a proposed \
Expand Down
6 changes: 4 additions & 2 deletions rrplugins/plugins/nelder_mead/nmNelderMead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ namespace nmfit
mProperties.add(&mStatusMessage);

//Allocate model and Residuals data
mResidualsData.setValue(new TelluriumData());
mModelData.setValue(new TelluriumData());
TelluriumData residualsData;
mResidualsData.setValue(&residualsData);
TelluriumData modelData;
mModelData.setValue(&modelData);

mHint = "Parameter fitting using the Nelder-Mead algorithm";
mDescription = "The Nelder-Mead plugin is used to fit a proposed \
Expand Down
14 changes: 7 additions & 7 deletions wrappers/C/rrc_cpp_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ RRStringArrayPtr createList(const rrc::StringList& sList)
RRStringArray* list = new RRStringArray;
list->Count = sList.Count();

if (list->Count)
list->String = new char*[list->Count];
if (list->Count) {
list->String = new char *[list->Count];
for(int i = 0; i < list->Count; i++)
{
list->String[i] = rr::createText(sList[i]);
}
}
else
list->String = NULL;

for(int i = 0; i < list->Count; i++)
{
list->String[i] = rr::createText(sList[i]);
}
return list;
}

Expand Down

0 comments on commit 26dc2b4

Please sign in to comment.