Skip to content

8365180: Remove sun.awt.windows.WInputMethod.finalize() #26706

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

Closed
wants to merge 2 commits into from

Conversation

prrace
Copy link
Contributor

@prrace prrace commented Aug 8, 2025

Remove finalize() from WInputMethod.java - it is used to free a native id.
Also the reason dispose() didn't free it seems no longer relevant.
Although I did see (when instrumenting) that dispose() was called when I disposed() the Frame referencing the IM,
I don't know if I can be sure that is always done. So safest to add the Disposer in case it isn't.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8365180: Remove sun.awt.windows.WInputMethod.finalize() (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26706/head:pull/26706
$ git checkout pull/26706

Update a local copy of the PR:
$ git checkout pull/26706
$ git pull https://git.openjdk.org/jdk.git pull/26706/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26706

View PR using the GUI difftool:
$ git pr show -t 26706

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26706.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 8, 2025

👋 Welcome back prr! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 8, 2025

@prrace This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8365180: Remove sun.awt.windows.WInputMethod.finalize()

Reviewed-by: serb, azvegint

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 158 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title 8365180 8365180: Remove sun.awt.windows.WInputMethod.finalize() Aug 8, 2025
@openjdk
Copy link

openjdk bot commented Aug 8, 2025

@prrace The following labels will be automatically applied to this pull request:

  • client
  • i18n

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added client [email protected] i18n [email protected] rfr Pull request is ready for review labels Aug 8, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 8, 2025

Webrevs

// Due to a memory management problem in Windows 98, we should retain
// the native input context until this object is finalized. So do
// nothing here.
disposerRecord.dispose();
Copy link
Member

Choose a reason for hiding this comment

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

The native dispose will call ImmDestroyContext which is specified as:

However, before calling ImmDestroyContext, the application must remove the input context from any association with windows in the thread by using the ImmAssociateContext function.

I did not check the code, but maybe we are calling ImmAssociateContext in the wrong moment and have to postpone this "dispose" until all the object is really unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to be done by a native method WInputMethod.disableNativeIME(..) - which sets the IME for the window to NULL. The native method is called from various places in WInputMethod.
I can't see that dispose or finalize would do anything to ensure that is called, or introduce any timing issues either way. So if there's a problem it is un-related.

Copy link
Member

Choose a reason for hiding this comment

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

I can't see that dispose or finalize would do anything to ensure that is called, or introduce any timing issues either way. So if there's a problem it is un-related.

Maybe disableNativeIME is called when the top level Window is disposed, and after that there are no more references to WInputMethod? In that case, it is safe to call it from finalize(), but it might not be as safe to call it from dispose(), which mighe be called before "WInputMethod.disableNativeIME"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe disableNativeIME is called when the top level Window is disposed,

No it is not.

Nothing calls disableNativeIME when the window is disposed. I tried this with the disposer removed and finalize restored just to be sure.

Also it needs to be called 'early' but not 'too early' during WIndow.dispose()
If the input method is active when you try to dispose it via APIs like disableNativeIM() then an exception is thrown.

If you call it too late - after removeNotify() then it will not be sent because the peer is now nulled out.

src/java.desktop/windows/native/libawt/windows/awt_Toolkit.cpp
@@ -1082,6 +1082,7 @@ LRESULT CALLBACK AwtToolkit::WndProc(HWND hWnd, UINT message,
AwtComponent* comp = (AwtComponent*)JNI_GET_PDATA(peer);
if (comp != NULL)
{
comp->SetInputMethod(self, useNativeCompWindow);
comp->ImmAssociateContext((HIMC)((intptr_t)context));
}

Since we never call it, then this migration to disposer changes nothing.
If we do call it I think it should be called in WInputMethod.removeNotify() which will clearly be before dispose().
I will add that and we'll be better off then we were before this change.

I also found that if the window has focus when it is dispose()'d that it will receive a focus lost event which ironically creates a new WInputMethod because Window.dispose() had nulled out the existing one

Either don't null out the disposed one, or do something else to avoid re-creation, or decide that it is harmless .
For now I will leave that as it seems to be harmless, pre-existing and un-related to this change, although a little wasteful. It also would be more than a windows-only change.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 20, 2025
@prrace
Copy link
Contributor Author

prrace commented Aug 22, 2025

/integrate

@openjdk
Copy link

openjdk bot commented Aug 22, 2025

Going to push as commit 8e44856.
Since your change was applied there have been 160 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 22, 2025
@openjdk openjdk bot closed this Aug 22, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 22, 2025
@openjdk
Copy link

openjdk bot commented Aug 22, 2025

@prrace Pushed as commit 8e44856.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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

Successfully merging this pull request may close these issues.

3 participants