﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5406	Memory Leak in memory_pool	schmitz.basti@…	Lennart Ochel	"The following model leaks memory every time the function `fmi2GetReal` is called:

{{{
model LeakyModel
  parameter Integer arraySize = 150;
  input Real[arraySize] inputArray;
  output Real[arraySize] outputArray;
equation
  outputArray = functions.leakyCopy(arraySize, inputArray);
end LeakyModel;

package functions
  function leakyCopy
    input Integer s;
    input Real[s] arrayToCopy;
    output Real[s] copiedArray;
    algorithm
    copiedArray := arrayToCopy;
  end leakyCopy;
end functions;
}}}

How to reproduce:
System: Ubuntu Linux 18.04 LTS
OpenModelica version: 1.13.2 and current dev (OMCompiler v1.14.0-dev.177+gb70342a5e) both show the same behavior

Build FMU via oms script:
{{{
loadModel(Modelica);getErrorString();
loadFiles(fileNames={""LeakyModel.mo""});getErrorString();
buildModelFMU(LeakyModel, platforms={""static""}, includeResources=true); getErrorString();
}}}

I used [https://github.com/NTNU-IHB/FMI4cpp FMI4cpp] to load the FMU and call `fmi2SetReal` and `fmi2GetReal` in a loop, like this:

{{{
int main() {
    fmi2Fmu fmu(""../resources/LeakyModel.fmu"");
    auto me_fmu = fmu.asModelExchangeFmu();
    auto md = me_fmu->getModelDescription();

    logger::info(""Name={}, start={}"", var.name(), var.start().value_or(0));

    auto slave = me_fmu->newInstance();
    logger::info(""modelIdentifier={}"", slave->getModelDescription()->modelIdentifier);

    slave->enterInitializationMode();
    slave->exitInitializationMode();

    fmi4cpp::fmi4cppInteger arraySize;
    slave->readInteger(md->getVariableByName(""arraySize"").valueReference, arraySize);

    logger::info(""The array is of size "" + std::to_string(arraySize));

    auto inReference = md->getVariableByName(""inputArray[1]"").valueReference;
    auto outReference = md->getVariableByName(""outputArray[1]"").valueReference;

    fmi4cpp::fmi4cppReal r;
    for(int i = 0; i < 1000000; ++i){
        slave->writeReal(inReference, i);
        slave->readReal(outReference, r);
      logger::info(""Read: "" + std::to_string(r));
      std::this_thread::sleep_for(10ms);
    }

    return 0;
}
}}}
(The leak is small, but this model is just a trimmed down version to showcase the faulty behavior a bigger one I am using shows)

The valgrind output looks like this:
{{{
==26715== 62,914,560 bytes in 4 blocks are still reachable in loss record 148 of 148
==26715==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26715==    by 0x7901C7E: pool_expand (memory_pool.c:103)
==26715==    by 0x7901C7E: pool_malloc (memory_pool.c:113)
==26715==    by 0x7905480: alloc_real_array (real_array.c:85)
==26715==    by 0x78315CD: omc_functions_leakyCopy (in /tmp/fmi4cpp_LeakyModel_17449512/binaries/linux64/LeakyModel.so)
==26715==    by 0x7831375: LeakyModel_eqFunction_2 (in /tmp/fmi4cpp_LeakyModel_17449512/binaries/linux64/LeakyModel.so)
==26715==    by 0x78318D1: LeakyModel_functionAlgebraics (in /tmp/fmi4cpp_LeakyModel_17449512/binaries/linux64/LeakyModel.so)
==26715==    by 0x78131F1: fmi2GetReal (in /tmp/fmi4cpp_LeakyModel_17449512/binaries/linux64/LeakyModel.so)
==26715==    by 0x150DF8: fmi4cpp::fmi2::fmi2Library::readReal(void*, unsigned int, double&) (fmi2Library.cpp:199)
==26715==    by 0x156DDC: fmi4cpp::AbstractFmuInstance<fmi4cpp::fmi2::fmi2ModelExchangeLibrary, fmi4cpp::fmi2::ModelExchangeModelDescription>::readReal(unsigned int, double&) (AbstractFmuInstance.hpp:145)
==26715==    by 0x11C803: main (fmu_test.cpp:75)
}}}

The memory pool just keeps getting bigger and bigger and I see no way to let the FMU clean up. 

Am I doing something fundamentally wrong or is this a bad memory leak?
"	defect	closed	high	Future	Run-time	v1.14.0-dev-nightly	fixed		
