open-simulation-platform / libcosim

OSP C++ co-simulation library
https://open-simulation-platform.github.io/libcosim
Mozilla Public License 2.0
61 stars 10 forks source link

Bug/646 observer memory leaks #647

Closed restenb closed 3 years ago

restenb commented 3 years ago

This PR aims to fix the memory leaks in time_series_observer.cpp and file_observer.cpp.

For file_observer, the std::stringstream ss_ that was "collecting" data to write to file has been reduced in scope to a local variable in the functions that used it.

For time_series_observer, the culprit was the zero argument constructor requesting a buffer size of 0, which bypassed the adjustIfFulltest and led to constantly increasing buffer sizes for all variable types. The zero argument constructor will now instead provide a default buffer size of 10000 samples. A zero sample observer is no longer allowed and will throw, the minimum buffer size is 1.

The changes have been tested with cosim-cli and memory use found to be stable at 5-6 MB over a 6000 second simulation of the techlaunch-tutorial configuration while logging everything. Running the same simulation on the current master branch leads to memory usage steadily climbing to 10 times that.