Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when unable to terminate FMU #146

Closed
stephen-ryan-ansys opened this issue Sep 24, 2024 · 1 comment · Fixed by #148
Closed

Memory leak when unable to terminate FMU #146

stephen-ryan-ansys opened this issue Sep 24, 2024 · 1 comment · Fixed by #148

Comments

@stephen-ryan-ansys
Copy link

When using the library, I am encountering a memory leak in the situation where the program exits when the FMU is not in a state where terminate is allowed to be called. For example, if an exception is thrown after enter_initialization_mode() is called, but before exit_initialization_mode() is called. I looked at the destructor in the fmu_instance_base class, and it is calling terminate(), which attempts to terminate the FMU and free the resources:

    bool terminate(bool freeInstance)
    {
        if (!this->terminated_) {
            this->terminated_ = true;
            if (!library_->terminate(c_)) {
                return false;
            }
            this->free_instance();
        }
        return true;
    }

However, if the library fails to terminate the FMU, the function returns early with false, and the free_instance() function is never called. This corresponds to fmi2FreeInstance, which should be callable even when the FMU is not terminated. It could be changed to something like the following.

    bool terminate(bool freeInstance)
    {
        if (!this->terminated_) {
            this->terminated_ = library_->terminate(c_);
        }
        this->free_instance();
        return this->terminated_;
    }

Or the call to free_instance can be removed from terminate and simply called from the destructor like:

    ~fmu_instance_base()
    {
        terminate();
        free_instance();
    }
@markaren
Copy link
Member

I'll see if I can look into this not to long into the future. I notice that bool freeInstance is not actually used, so the code path needs some cleanup regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants