Skip to content

Commit

Permalink
Fix memory leak of model info xml data (#7473)
Browse files Browse the repository at this point in the history
- Backporting #7471 and #7464
  • Loading branch information
AnHeuermann committed May 18, 2021
1 parent f102495 commit 03f0da6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
13 changes: 13 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation/simulation_info_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ void modelInfoInit(MODEL_DATA_XML* xml)
// fprintf(stderr, "Loaded the JSON (%ld kB)...\n", (long) (s.st_size+1023)/1024);
}
#endif
assert(xml->functionNames == NULL);
xml->functionNames = (FUNCTION_INFO*) calloc(xml->nFunctions, sizeof(FUNCTION_INFO));
assert(xml->equationInfo == NULL);
xml->equationInfo = (EQUATION_INFO*) calloc(1+xml->nEquations, sizeof(EQUATION_INFO));
xml->equationInfo[0].id = 0;
xml->equationInfo[0].profileBlockIndex = -1;
Expand All @@ -410,6 +412,17 @@ void modelInfoInit(MODEL_DATA_XML* xml)
#endif
}

/**
* @brief Deinitialize memory allocated by modelInfoInit
*
* @param xml Pointer to model info xml data.
*/
void modelInfoDeinit(MODEL_DATA_XML* xml)
{
free(xml->functionNames); xml->functionNames = NULL;
free(xml->equationInfo); xml->equationInfo = NULL;
}

FUNCTION_INFO modelInfoGetFunction(MODEL_DATA_XML* xml, size_t ix)
{
if(xml->functionNames == NULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
extern "C" {
#endif

extern FUNCTION_INFO modelInfoGetFunction(MODEL_DATA_XML*,size_t);
extern void modelInfoInit(MODEL_DATA_XML*);
extern EQUATION_INFO modelInfoGetEquation(MODEL_DATA_XML*,size_t);
extern EQUATION_INFO modelInfoGetEquationIndexByProfileBlock(MODEL_DATA_XML*,size_t);
void modelInfoInit(MODEL_DATA_XML* xml);
void modelInfoDeinit(MODEL_DATA_XML* xml);
FUNCTION_INFO modelInfoGetFunction(MODEL_DATA_XML* xml, size_t ix);
EQUATION_INFO modelInfoGetEquation(MODEL_DATA_XML* xml, size_t ix);
EQUATION_INFO modelInfoGetEquationIndexByProfileBlock(MODEL_DATA_XML* xml, size_t ix);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation/solver/model_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,9 @@ void deInitializeDataStruc(DATA *data)
FREE_VARS(nSensitivityVars, realSensitivityData)
}

/* Free model info xml data */
modelInfoDeinit(&(data->modelData->modelDataXml));

TRACE_POP
}

Expand Down

0 comments on commit 03f0da6

Please sign in to comment.