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

fix(Windows): WindowsInputPane memory leak #16916

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

workgroupengineering
Copy link
Contributor

What does the pull request do?

Fix memory eak on windows platform

What is the current behavior?

see #16900

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

Closes #16900

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0051637-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Comment on lines +98 to +102
{
lock (_lock)
{
DestroyIfNeeded();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be problematic to completely destroy this object, as it still might be used by native side.
To avoid Window memory leak, it will be enough to nullify "_pane" reference on disposal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did as you suggest, WindowsInputPane is collected by GC but WindowsInputPane+Handler is reatain because Shadow keeps reference to it.

immagine

immagine

I tried clear both references but WindowsInputPane+Handler is still active

    private class Handler : CallbackBase, IFrameworkInputPaneHandler
    {
        private WindowsInputPane _pane;

        public Handler(WindowsInputPane pane) => _pane = pane;
        public void Showing(UnmanagedMethods.RECT* rect, int _) => _pane.OnStateChanged(true, *rect);
        public void Hiding(int fEnsureFocusedElementInView) => _pane.OnStateChanged(false, null);

        public void Clear()
        {
            lock (this)
            {
                _pane = null!;
                Shadow = null;
            }
        }
    }
}

immagine

Potentially all of the following classes are affected by the same problem

immagine

I think at this point that the best thing to do is to do two PRs

  • one to change the behavior of CallbackBase
  • the other to clear the reference to WindowsInputPane

@maxkatz6 , @kekekeks What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially all of the following classes are affected by the same problem

It needs to be confirmed whether it's MicroCOM holds these references when it shouldn't, or native side haven't released them. And I feel like it's the second case.
Either way, I don't think it's in the scope of this PR.

Just the fact that Handler doesn't leak whole Window with it is enough for this PR.

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

Successfully merging this pull request may close these issues.

3 participants