]> gerrit.simantics Code Review - simantics/fmil.git/blobdiff - org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java
Ensure unloadFMU is called only if FMU instance (pointer) exists
[simantics/fmil.git] / org.simantics.fmil.core / src / org / simantics / fmil / core / FMIL.java
index 06d1fd2b2b97acf142db5532f2c547337b2f4966..ec63279c89feb1e982b3e79e254f77713afccac6 100644 (file)
@@ -39,6 +39,7 @@ public class FMIL {
      */
     private static int      OK                  = 0;
     private static int      ERROR               = 1;
+    private static int      PENDING                    = 2;
     private static String   UNSATISFIED_LINK    = "Method not found. DLL might not be loaded properly.";
     public static final String TEMP_FMU_DIRECTORY_NAME = "fmil";
     public static String    TEMP_FMU_COMMON_DIRECTORY;
@@ -183,7 +184,7 @@ public class FMIL {
      * @throws FMILException
      */
     private int fmuN = 0;
-    private boolean fmuLoaded = false;
+    private boolean instantiated = false;
     public void loadFMUFile(String path) throws FMILException {
 
         if (!Files.exists(Paths.get(path)))
@@ -228,8 +229,8 @@ public class FMIL {
                 for(int i=0;i<variableNames.length;i++) {
                        variableMap.put(variableNames[i], variableReferences[i]);
                 }
-
-                fmuLoaded = true;
+                
+                instantiated = false;
             } catch (UnsatisfiedLinkError err) {
                 throw new FMILException(UNSATISFIED_LINK, err);
             } catch (Exception e) {
@@ -277,9 +278,11 @@ public class FMIL {
             try {
 
                 int ret = instantiateSimulation_(getModelIDNew()); 
-                if(ret != OK)
+                if(ret != OK) {
                     LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
-
+                } else {
+                       instantiated = true;
+                }
             } catch (FMILException e) {
                 throw e;
             } catch (UnsatisfiedLinkError err) {
@@ -513,10 +516,11 @@ public class FMIL {
 
             try {
 
-                int ret = simulateStep_(getModelIDNew()); 
-                if(ret != OK)
+                int ret = simulateStep_(getModelIDNew()); //0 is ok, 1 is error, 2 is pending
+                if(ret == PENDING)
+                    LOGGER.warn("Pending status return from FMU. This is not implemented in our Simulator yet!");
+                else if(ret != OK)
                     LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
-
             } catch (FMILException e) {
                 throw e;
             } catch (UnsatisfiedLinkError err) {
@@ -570,11 +574,15 @@ public class FMIL {
             try {
 
                 unlockFMUDirectory();
-                if(fmuLoaded) {
+                
+                // Many FMUs will not correctly clean-up during unload if the simulation isn't instantiated
+                // Essentially we'd likely be passing an invalid pointer to the FMU to cleanup, causing native c-level crashes.
+                
+                if(instantiated) {
                     int ret = unloadFMU_(getModelIDNew()); 
                     if(ret != OK)
                         LOGGER.warn("Function return value != OK, an exception should have been thrown from native code!");
-                    fmuLoaded = false;
+                    instantiated = false;
                 }
                 removeFMUDirectoryContents();