Menu ▾ ▴

#2455 Bug: Window Leak

KeePass_2.x
open
nobody
None
5
2 days ago
2 days ago
No

On Linux with Mono, each time the Edit Entry or Options dialog is opened and closed, X11 windows are left behind permanently (they are never destroyed until KeePass exits). In a long-running session this accumulates into thousands of hidden windows owned by KeePass, which slows the X server (window tree walks on pointer motion, compositing, etc.).

Observed in a real session: KeePass 2.57 (Debian package) running for ~148 days owned 4,194 X windows (1,515 of them hidden top-level windows). Restarting KeePass dropped this to 272.

The behavior is unchanged in 2.62. The root cause is partly Mono bugs and partly KeePass code paths that trigger them; the attached patch works around all of the KeePass-side triggers (Options: 16 → 0 leaked windows per open; Edit Entry: 28 → 6).

Environment
KeePass 2.62 built from the official source (Build/PrepMonoDev.sh + xbuild), also reproduced with the Debian keepass2 2.57 package
Mono 6.8.0.105 (Debian 12), X11 (Xorg and Xvfb), no plugins
Test database: 30 entries, default configuration

Measurements (stock 2.62, no plugins)
X windows created and not destroyed, per open/close of a dialog (×4, averaged):

Action, vs leaks
Main menu, entry context menu, toolbar tooltips --> Leaked: 0
Tools → Options, then Esc --> Leaked:16
Edit Entry (double-click), then Esc --> Leaked: 28

Each leaked window was identified by enumerating Mono's internal Hwnd table


Mono's ContextMenuStrip(IContainer container) constructor ignores container (upstream source: // TODO: handle 'container' argument). Therefore new CustomContextMenuStripEx(this.components) is never added to components, components.Dispose() never disposes it, and since Mono creates the menu's X window eagerly in the constructor, the window leaks.

Edit Entry: m_ctxDefaultTimes, m_ctxStr, m_ctxAutoType, m_ctxHst, m_ctxTools, m_ctxBinAttach → 12 windows per open
Affects all 10 designer usages of CustomContextMenuStripEx(IContainer).
Fix (CustomContextMenuStripEx.cs): in the IContainer constructor, if((container != null) && (this.Container == null)) container.Add(this); On .NET, this.Container is already set by the base constructor, so this is a no-op there.


RichTextBoxContextMenu.Detach() drops the menu without disposing it
Detach() sets m_ctx = null but never calls m_ctx.Dispose(). Used by Edit Entry (notes), Edit String, Edit Auto-Type Item, Data Viewer/Editor, main window preview. → 2 windows per open of each of those dialogs.

Fix (RichTextBoxContextMenu.cs): m_ctx.Dispose(); before m_ctx = null;.


. HotKeyControlEx creates an empty ContextMenuStrip that is never disposed
The constructor does this.ContextMenuStrip = new ContextMenuStrip(); // No context menu. The Options dialog has 4 hotkey boxes → 8 windows per open.

Fix (HotKeyControlEx.cs): keep a reference and dispose it in Dispose(bool).


. Native handles forced for controls on tab pages that are never shown
If a control's Handle is created while its parent TabPage has no handle yet, Mono parents the X window to its hidden "FosterParent" window. If the tab is never shown, the window is never reparented into the dialog, so destroying the dialog does not destroy it. Mono marks the Hwnd as a zombie, but the X window remains forever (Mono bug)

Fixes (UIUtil.cs):

GetScrollInfo: if !lv.IsHandleCreated, return new UIScrollInfo(0, 0, (lv.Items.Count > 0) ? 0 : -1). A list without a native window has never been displayed, so it is not scrolled.
SetExplorerTheme(Control, bool): only call SetExplorerTheme(c.Handle) if !NativeLib.IsUnix().
AdjustDropDownWidth: only call Screen.FromControl(cmb) if cmb.IsHandleCreated, otherwise use Screen.PrimaryScreen.

On Windows, these changes keep the same behavior. The only exception is AdjustDropDownWidth for a combo box whose window does not exist yet: it now uses the primary screen's working-area width as the cap. Previously, creating the handle early on a not-yet-positioned dialog gave effectively the same result.


After the patch, Edit Entry still leaks 6 windows per open, all from Mono internals:

Submenus of the designer menus ("Insert Field Reference", "Move To"): Mono's ToolStripDropDownItem.Dispose does not dispose the item's DropDown.
The expiry DateTimePicker: Mono never disposes its MonthCalendar drop-down popup.


Results with the patch (2.62 + patch, no plugins)

Window leaks before/after
Options Before: 16 -> After Patch: 0
Edit Entry Before: 28 -> After Patch: 6 (due to Mono Bugs)

1 Attachments

Discussion


Log in to post a comment.