modelon-community / fmi-library

C library for importing FMUs
Other
115 stars 34 forks source link

The segmentation fault occurs in the fmi2_SI_base_unit_exp_to_string function. #118

Closed YanceyYq closed 7 months ago

YanceyYq commented 7 months ago

When using the fmi2_import_instantiate function, a segmentation fault occurs within the fmi2_SI_base_unit_exp_to_string function.

PeterMeisrimelModelon commented 7 months ago

Can you provide us with some more information to reproduce the issue?

YanceyYq commented 7 months ago

this is information const fmi2_string_t instanceName = "MyModelInstance"; const fmi2_type_t fmuType = fmi2_cosimulation; const fmi2_string_t fmuResourceLocation = NULL; const fmi2_boolean_t visible = fmi2_false;

    jm_status_enu_t result = fmi2_import_instantiate(fmu2, instanceName, fmuType, fmuResourceLocation, visible);

The above implementation encountered a segmentation fault at the fmi2_SI_base_unit_exp_to_string function when debugging the program at the fmi2_import_instantiate step.

PeterMeisrimelModelon commented 7 months ago

It is possible to share more information, e.g., a full backtrace from the segmentation fault?

It seems very odd that fmi2_SI_base_unit_exp_to_stringwould cause a segfault inside a call of fmi2_import_instantiate, since there is no direct connection, unless via callbacks.

YanceyYq commented 7 months ago

After using the latest version of fmilibrary, when I use the fmi2_import_instantiate function, the parameter fmi2_import_t fmu has a value. However, when it runs to the fmi2_component_t fmi2_capi_instantiate(fmi2_capi_t fmu, fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuGUID, fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible, fmi2_boolean_t loggingOn) function, the value of fmu becomes 0x0.

PeterMeisrimelModelon commented 7 months ago

Can you provide a full call sequence to provide context on what you found thus far? I cannot reliably follow what you are doing with the information provided.

One possible option that I am seeing is that your fmureturned null on the fmi2Instantiatecall.

YanceyYq commented 7 months ago

step1:use fmi2_import_instantiate(this step fmu have value) const fmi2_string_t instanceName = "MyModelInstance"; const fmi2_type_t fmuType = fmi2_cosimulation; const fmi2_string_t fmuResourceLocation = NULL; const fmi2_boolean_t visible = fmi2_false;

    jm_status_enu_t result = fmi2_import_instantiate(fmu2, instanceName, fmuType, fmuResourceLocation, visible);

step2: in fmi2_import_instantiate(this step fmu have value) jm_status_enu_t fmi2_import_instantiate(fmi2_import_t fmu, fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible) { fmi2_string_t fmuGUID = fmi2_import_get_GUID(fmu); fmi2_boolean_t loggingOn = (fmu->callbacks->log_level > jm_log_level_nothing); fmi2_component_t c; if(!fmuResourceLocation) fmuResourceLocation = fmu->resourceLocation; c = fmi2_capi_instantiate(fmu -> capi, instanceName, fmuType, fmuGUID, fmuResourceLocation, visible, loggingOn); if (c == NULL) { return jm_status_error; } else { return jm_status_success; } } step3:in fmi2_capi_instantiate(this step fmu is 0x0) fmi2_component_t fmi2_capi_instantiate(fmi2_capi_t fmu, fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuGUID, fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible, fmi2_boolean_t loggingOn) { return fmu->c = fmu->fmi2Instantiate(instanceName, fmuType, fmuGUID, fmuResourceLocation, &fmu->callBackFunctions, visible, loggingOn); } in this function fmu is 0x0;

PeterMeisrimelModelon commented 7 months ago

What are the steps before this?

I'm assuming fmu2 = fmi2_import_parse_xml(...); but it sounds like you might be missing a call to fmi2_import_create_dllfmu?

You can also check fmi2_import_cs_test.c for reference on all the steps involved in loading a FMI2 CS FMU.

YanceyYq commented 7 months ago

before is step1: fmi_import_context_t context = fmi_import_allocate_context(&callbacks); step2: fmi_version_enu_t version = fmi_import_get_fmi_version(context, Filename, tempPath); step3: fmi2_import_t fmu2 = fmi2_import_parse_xml(context, tempPath, NULL);

PeterMeisrimelModelon commented 7 months ago

You seem to be missing an fmi2_import_create_dllfmu call, e.g., fmi2_import_create_dllfmu(fmu, fmi2_fmu_kind_cs, NULL) after fmi2_import_parse_xml(...) and before fmi2_import_instantiate(...).

Can you try and report back?

Edit: If this is the issue, there is a fix incoming: https://github.com/modelon-community/fmi-library/pull/119

With that fix, you should get an error rather than a segmentation fault.

YanceyYq commented 7 months ago

I solved the problem by using fmi2_import_create_dllfmu. Thank you for your help.