Skip to content

Commit

Permalink
Fix memory leak of model info xml data (#7464)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnHeuermann committed May 14, 2021
1 parent 7543714 commit 698787c
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
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->functionNames); xml->functionNames = NULL;

This comment has been minimized.

Copy link
@adeas31

adeas31 May 17, 2021

Member

Are you really trying to free xml->functionNames twice? Maybe you intend to free xml->equationInfo.

This comment has been minimized.

Copy link
@AnHeuermann

AnHeuermann May 18, 2021

Author Member

Ups. Fixing in #7471

}

FUNCTION_INFO modelInfoGetFunction(MODEL_DATA_XML* xml, size_t ix)
{
if(xml->functionNames == NULL)
Expand Down
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
Expand Up @@ -1280,6 +1280,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 698787c

Please sign in to comment.