Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 08c3710

Browse files
adrpoOpenModelica-Hudson
authored andcommitted
copy values of fmi2CallbackFunctions in fmi2Instantiate
- copy fmi2CallbackFunctions* functions in fmi2Instantate as it could be freed by the caller environment after the call Belonging to [master]: - #2759
1 parent 06d58cd commit 08c3710

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
396396

397397
comp->instanceName = (fmi2String)functions->allocateMemory(1 + strlen(instanceName), sizeof(char));
398398
comp->GUID = (fmi2String)functions->allocateMemory(1 + strlen(fmuGUID), sizeof(char));
399+
comp->functions = (fmi2CallbackFunctions*)functions->allocateMemory(1, sizeof(fmi2CallbackFunctions));
399400
fmudata = (DATA *)functions->allocateMemory(1, sizeof(DATA));
400401
modelData = (MODEL_DATA *)functions->allocateMemory(1, sizeof(MODEL_DATA));
401402
simInfo = (SIMULATION_INFO *)functions->allocateMemory(1, sizeof(SIMULATION_INFO));
@@ -423,7 +424,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
423424
}
424425
}
425426

426-
if (!comp || !comp->instanceName || !comp->GUID) {
427+
if (!comp || !comp->instanceName || !comp->GUID || !comp->functions) {
427428
functions->logger(functions->componentEnvironment, instanceName, fmi2Error, "error", "fmi2Instantiate: Out of memory.");
428429
return NULL;
429430
}
@@ -435,7 +436,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
435436
strcpy((char*)comp->instanceName, (const char*)instanceName);
436437
comp->type = fmuType;
437438
strcpy((char*)comp->GUID, (const char*)fmuGUID);
438-
comp->functions = functions;
439+
memcpy((void*)comp->functions, (void*)functions, sizeof(fmi2CallbackFunctions));
439440
comp->componentEnvironment = functions->componentEnvironment;
440441
comp->loggingOn = loggingOn;
441442
comp->state = modelInstantiated;
@@ -500,6 +501,7 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
500501
void fmi2FreeInstance(fmi2Component c)
501502
{
502503
ModelInstance *comp = (ModelInstance *)c;
504+
fmi2CallbackFreeMemory freeMemory = comp->functions->freeMemory;
503505
int meStates = modelInstantiated|modelInitializationMode|modelEventMode|modelContinuousTimeMode|modelTerminated|modelError;
504506
int csStates = modelInstantiated|modelInitializationMode|modelEventMode|modelContinuousTimeMode|modelTerminated|modelError;
505507

@@ -537,8 +539,9 @@ void fmi2FreeInstance(fmi2Component c)
537539
/* free instanceName & GUID */
538540
if (comp->instanceName) comp->functions->freeMemory((void*)comp->instanceName);
539541
if (comp->GUID) comp->functions->freeMemory((void*)comp->GUID);
542+
if (comp->functions) comp->functions->freeMemory((void*)comp->functions);
540543
/* free comp */
541-
comp->functions->freeMemory(comp);
544+
freeMemory(comp);
542545
}
543546

544547
fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance, fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)

SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
extern "C" {
3939
#endif
4040

41+
/* make sure all compiler use the same alignment policies for structures */
42+
#if defined _MSC_VER || defined __GNUC__
43+
#pragma pack(push,8)
44+
#endif
45+
4146
// categories of logging supported by model.
4247
// Value is the index in logCategories of a ModelInstance.
4348
#define LOG_EVENTS 0
@@ -88,6 +93,11 @@ typedef struct {
8893
ANALYTIC_JACOBIAN* fmiDerJac;
8994
} ModelInstance;
9095

96+
/* reset alignment policy to the one set before reading this file */
97+
#if defined _MSC_VER || defined __GNUC__
98+
#pragma pack(pop)
99+
#endif
100+
91101
#ifdef __cplusplus
92102
}
93103
#endif

0 commit comments

Comments
 (0)