From b52728a62ae45a4f88126c5f4f6fafb04534daf5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 21 Jun 2023 18:20:38 +0200 Subject: [PATCH] Fix memory leak of wxClipboard data on exit When wxClipboard is destroyed as part of the program shutdown, gdk_selection_owner_get() doesn't return our clipboard widget as owner any more, so we don't reset the owner when Clear() is called and hence never free the data. Do it explicitly if we don't have clipboard ownership in Clear() any longer to avoid memory leaks -- even though they are mostly harmless (as they happen only once, on exit), they still show up in LSAN and similar tools reports. --- src/gtk/clipbrd.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/gtk/clipbrd.cpp b/src/gtk/clipbrd.cpp index a3be262f7e..91afdcd344 100644 --- a/src/gtk/clipbrd.cpp +++ b/src/gtk/clipbrd.cpp @@ -587,6 +587,15 @@ void wxClipboard::Clear() // it will free our data SetSelectionOwner(false); } + else + { + // We need to free our data directly to avoid leaking memory. + delete m_dataPrimary; + m_dataPrimary = nullptr; + + delete m_dataClipboard; + m_dataClipboard = nullptr; + } m_targetRequested = nullptr; m_formatSupported = false;