From 985a64f11da1d4f2d524e042372a3cd7757f6731 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 21 Feb 2026 15:17:31 +0100 Subject: [PATCH] Fix memory leaks reported by Xcode Memory Analysis Mostly just add autorelease to avoid various objects being leaked, but also add manual calls to release for the code which is not necessarily executed inside a GUI application and so can't rely on autorelease pool existence. --- src/osx/cocoa/dataview.mm | 2 +- src/osx/cocoa/statbox.mm | 2 +- src/osx/core/uilocale.mm | 8 ++++++-- src/osx/webview_webkit.mm | 7 ++++--- src/stc/PlatWXcocoa.mm | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index a8daf00882..87ce901da5 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -2019,7 +2019,7 @@ void wxCocoaDataViewControl::InitOutlineView(long style) NSTableHeaderView* header = nil; if ( !(style & wxDV_NO_HEADER) ) { - header = [[wxDVCNSHeaderView alloc] initWithDVC:GetDataViewCtrl()]; + header = [[[wxDVCNSHeaderView alloc] initWithDVC:GetDataViewCtrl()] autorelease]; } [m_OutlineView setHeaderView:header]; diff --git a/src/osx/cocoa/statbox.mm b/src/osx/cocoa/statbox.mm index b49203df2b..7af80028cb 100644 --- a/src/osx/cocoa/statbox.mm +++ b/src/osx/cocoa/statbox.mm @@ -86,7 +86,7 @@ wxWidgetImplType* wxWidgetImpl::CreateGroupBox( wxWindowMac* wxpeer, NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; wxNSBox* v = [[wxNSBox alloc] initWithFrame:r]; NSSize margin = { 0.0, 0.0 }; - [v setContentView:[[wxNSBoxContentView alloc] init]]; + [v setContentView:[[[wxNSBoxContentView alloc] init] autorelease]]; [v setContentViewMargins: margin]; [v sizeToFit]; wxStaticBoxCocoaImpl* c = new wxStaticBoxCocoaImpl( wxpeer, v ); diff --git a/src/osx/core/uilocale.mm b/src/osx/core/uilocale.mm index 299abb505e..4d0dd87c92 100644 --- a/src/osx/core/uilocale.mm +++ b/src/osx/core/uilocale.mm @@ -303,7 +303,9 @@ wxUILocaleImplCF::GetMonthName(wxDateTime::Month month, wxDateTime::NameForm for } NSString* monthName = [monthNames objectAtIndex:(month)]; - return wxCFStringRef::AsString(monthName); + wxCFStringRef cf(monthName); + [df release]; + return cf.AsString(); } wxString @@ -348,7 +350,9 @@ wxUILocaleImplCF::GetWeekDayName(wxDateTime::WeekDay weekday, wxDateTime::NameFo } NSString* weekdayName = [weekdayNames objectAtIndex:(weekday)]; - return wxCFStringRef::AsString(weekdayName); + wxCFStringRef cf(weekdayName); + [df release]; + return cf.AsString(); } #endif // wxUSE_DATETIME diff --git a/src/osx/webview_webkit.mm b/src/osx/webview_webkit.mm index 838c7a276e..31df5c1e30 100644 --- a/src/osx/webview_webkit.mm +++ b/src/osx/webview_webkit.mm @@ -263,7 +263,7 @@ bool wxWebViewWebKit::Create(wxWindow *parent, { for (const auto& kv : m_handlers) { - [webViewConfig setURLSchemeHandler:[[WebViewCustomProtocol alloc] initWithHandler:kv.second.get()] + [webViewConfig setURLSchemeHandler:[[[WebViewCustomProtocol alloc] initWithHandler:kv.second.get()] autorelease] forURLScheme:wxCFStringRef(kv.first).AsNSString()]; } } @@ -334,7 +334,7 @@ bool wxWebViewWebKit::Create(wxWindow *parent, document.webkitFullscreenEnabled = true; \ "); [m_webView.configuration.userContentController addScriptMessageHandler: - [[WebViewScriptMessageHandler alloc] initWithWxWindow:this] name:@"__wxfullscreen"]; + [[[WebViewScriptMessageHandler alloc] initWithWxWindow:this]autorelease] name:@"__wxfullscreen"]; } m_UIDelegate = uiDelegate; @@ -620,7 +620,7 @@ void wxWebViewWebKit::RunScriptAsync(const wxString& javascript, void* clientDat bool wxWebViewWebKit::AddScriptMessageHandler(const wxString& name) { [m_webView.configuration.userContentController addScriptMessageHandler: - [[WebViewScriptMessageHandler alloc] initWithWxWindow:this] name:wxCFStringRef(name).AsNSString()]; + [[[WebViewScriptMessageHandler alloc] initWithWxWindow:this] autorelease] name:wxCFStringRef(name).AsNSString()]; // Make webkit message handler available under common name wxString js = wxString::Format("window.%s = window.webkit.messageHandlers.%s;", name, name); @@ -644,6 +644,7 @@ bool wxWebViewWebKit::AddUserScript(const wxString& javascript, WKUserScriptInjectionTimeAtDocumentStart : WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO]; [m_webView.configuration.userContentController addUserScript:userScript]; + [userScript release]; return true; } diff --git a/src/stc/PlatWXcocoa.mm b/src/stc/PlatWXcocoa.mm index 675fb99976..f3ca08b8ce 100644 --- a/src/stc/PlatWXcocoa.mm +++ b/src/stc/PlatWXcocoa.mm @@ -106,7 +106,7 @@ WX_NSWindow CreateFloatingWindow(wxWindow* wxWin) defer: NO]; [w setLevel:NSPopUpMenuWindowLevel]; [w setHasShadow:YES]; - [w setContentView:[[wxSTCPopupBaseView alloc] initWithwxWin:wxWin]]; + [w setContentView:[[[wxSTCPopupBaseView alloc] initWithwxWin:wxWin] autorelease]] ; return w; }