Skip to content

Commit

Permalink
copy values of fmi2CallbackFunctions in fmi2Instantiate
Browse files Browse the repository at this point in the history
- copy fmi2CallbackFunctions* functions in fmi2Instantate as it could be freed by the caller environment after the call

Belonging to [master]:
  - OpenModelica/OMCompiler#2759
  • Loading branch information
adrpo authored and OpenModelica-Hudson committed Nov 2, 2018
1 parent 06d58cd commit 08c3710
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c
Expand Up @@ -396,6 +396,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str

comp->instanceName = (fmi2String)functions->allocateMemory(1 + strlen(instanceName), sizeof(char));
comp->GUID = (fmi2String)functions->allocateMemory(1 + strlen(fmuGUID), sizeof(char));
comp->functions = (fmi2CallbackFunctions*)functions->allocateMemory(1, sizeof(fmi2CallbackFunctions));
fmudata = (DATA *)functions->allocateMemory(1, sizeof(DATA));
modelData = (MODEL_DATA *)functions->allocateMemory(1, sizeof(MODEL_DATA));
simInfo = (SIMULATION_INFO *)functions->allocateMemory(1, sizeof(SIMULATION_INFO));
Expand Down Expand Up @@ -423,7 +424,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
}
}

if (!comp || !comp->instanceName || !comp->GUID) {
if (!comp || !comp->instanceName || !comp->GUID || !comp->functions) {
functions->logger(functions->componentEnvironment, instanceName, fmi2Error, "error", "fmi2Instantiate: Out of memory.");
return NULL;
}
Expand All @@ -435,7 +436,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
strcpy((char*)comp->instanceName, (const char*)instanceName);
comp->type = fmuType;
strcpy((char*)comp->GUID, (const char*)fmuGUID);
comp->functions = functions;
memcpy((void*)comp->functions, (void*)functions, sizeof(fmi2CallbackFunctions));
comp->componentEnvironment = functions->componentEnvironment;
comp->loggingOn = loggingOn;
comp->state = modelInstantiated;
Expand Down Expand Up @@ -500,6 +501,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
void fmi2FreeInstance(fmi2Component c)
{
ModelInstance *comp = (ModelInstance *)c;
fmi2CallbackFreeMemory freeMemory = comp->functions->freeMemory;
int meStates = modelInstantiated|modelInitializationMode|modelEventMode|modelContinuousTimeMode|modelTerminated|modelError;
int csStates = modelInstantiated|modelInitializationMode|modelEventMode|modelContinuousTimeMode|modelTerminated|modelError;

Expand Down Expand Up @@ -537,8 +539,9 @@ void fmi2FreeInstance(fmi2Component c)
/* free instanceName & GUID */
if (comp->instanceName) comp->functions->freeMemory((void*)comp->instanceName);
if (comp->GUID) comp->functions->freeMemory((void*)comp->GUID);
if (comp->functions) comp->functions->freeMemory((void*)comp->functions);
/* free comp */
comp->functions->freeMemory(comp);
freeMemory(comp);
}

fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance, fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)
Expand Down
10 changes: 10 additions & 0 deletions SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.h
Expand Up @@ -38,6 +38,11 @@
extern "C" {
#endif

/* make sure all compiler use the same alignment policies for structures */
#if defined _MSC_VER || defined __GNUC__
#pragma pack(push,8)
#endif

// categories of logging supported by model.
// Value is the index in logCategories of a ModelInstance.
#define LOG_EVENTS 0
Expand Down Expand Up @@ -88,6 +93,11 @@ typedef struct {
ANALYTIC_JACOBIAN* fmiDerJac;
} ModelInstance;

/* reset alignment policy to the one set before reading this file */
#if defined _MSC_VER || defined __GNUC__
#pragma pack(pop)
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 08c3710

Please sign in to comment.