modelica / ModelicaStandardLibrary

Free (standard conforming) library to model mechanical (1D/3D), electrical (analog, digital, machines), magnetic, thermal, fluid, control systems and hierarchical state machines. Also numerical functions and functions for strings, files and streams are included.
https://doc.modelica.org
BSD 3-Clause "New" or "Revised" License
472 stars 169 forks source link

Resource leak in Modelica.Utilities.Examples.readRealParameterModel (long term edition) #1942

Open modelica-trac-importer opened 7 years ago

modelica-trac-importer commented 7 years ago

Modified by beutlich on 3 Mar 2016 07:59 UTC There are Modelica tools that evaluate parameter/variable expressions in the GUI before simulation. In case of Modelica.Utilities.Examples.readRealParameterModel this means that parameter J, _phirel0 and _wrel0 are determined by function calls of readRealParameter and thus by reading the values from file. It turns out that the file Examples_readRealParameters.txt is opened but not closed again.

See also #1940.


Reported by beutlich on 3 Mar 2016 07:52 UTC There are Modelica tools that evaluate parameter/variable expressions in the GUI before simulation. In case of Modelica.Utilities.Examples.readRealParameterModel this means that parameter J, _phirel0 and _wrel0 are determined by a function calls of readRealParameter and thus by reading the values from file. It turns out that the file Examples_readRealParameters.txt is opened but not closed again.

See also #1940.


Migrated-From: https://trac.modelica.org/Modelica/ticket/1942

modelica-trac-importer commented 7 years ago

Modified by beutlich on 3 Mar 2016 07:59 UTC

modelica-trac-importer commented 7 years ago

Modified by beutlich on 14 Mar 2016 13:57 UTC

AHaumer commented 5 years ago

This is still an issue. Well in the meantime I have my own implementation but I'd like to see a clean implementation by womeone who's really capable of C-programming.

  1. The file should be closed after reading.
  2. It should be a user's choice whether the parameters are read during translation or during execution.
MartinOtter commented 5 years ago

This is still an issue. Well in the meantime I have my own implementation but I'd like to see a clean implementation by womeone who's really capable of C-programming.

Why C? The example implementation is in Modelica (not in C).

1. The file should be closed after reading.

Yes. Just add Streams.close(fileName) at the end of the function.

2. It should be a user's choice whether the parameters are read during translation or during execution.

By purpose, function readRealParameter(..) was placed under Examples to demonstrate the concept. Sure, whenever something is not possible in current Modelica, one can implement this in C. But then there are a lot of issues with regards to support of different operating systems and different tool vendors. A pure Modelica implementation would be much more robust against these practical details and could be much easier adapted by a user to his/her needs.

Therefore, an alternative is to add a Modelica annotation, that performs the reading at run time and not during compile time (and just keep the current Modelica code).

AHaumer commented 5 years ago

@MartinOtter I have to admit that I didn't look into that functions for some time ... In the past, the bug was buried in readLine: external "C" string = ModelicaInternal_readLine(fileName,lineNumber,endOfFile) annotation(Library="ModelicaExternalC"); To be concise, I have my own clumsy C-implementation of readLine (without "cache").

Maybe the "close" is fixed now. The question is whether the file is read during translation or during execution.

I'd have to test whether it is possible to perform a simulation, change the file and re-run the simulation without re-translate. This is needed e.g. to perform optimization with an external optimzer like GenOpt.