Skip to content

Commit bb53282

Browse files
committed
Improve memory management and icon handling on macOS
- Remove targets, actions, and delegates before destruction to prevent callbacks after objects are destroyed - Add ARC usage notes to
1 parent e37ba95 commit bb53282

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/platform/macos/menu_macos.mm

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// Import Cocoa headers
99
#import <Cocoa/Cocoa.h>
1010

11+
// Note: This file assumes ARC (Automatic Reference Counting) is enabled
12+
// for proper memory management of Objective-C objects.
13+
1114
// Forward declarations - moved to global scope
1215
@interface MenuItemTarget : NSObject
1316
@property (nonatomic, assign) nativeapi::MenuItemID itemId;
@@ -175,6 +178,9 @@ - (void)menuDidClose:(NSMenu *)menu {
175178

176179
~Impl() {
177180
if (target_) {
181+
// Remove target and action to prevent callbacks after destruction
182+
[ns_menu_item_ setTarget:nil];
183+
[ns_menu_item_ setAction:nil];
178184
target_ = nil;
179185
}
180186
}
@@ -248,7 +254,7 @@ - (void)menuDidClose:(NSMenu *)menu {
248254
void MenuItem::SetIcon(const std::string& icon) {
249255
pimpl_->icon_ = icon;
250256

251-
NSImage* image = nullptr;
257+
NSImage* image = nil;
252258

253259
// Check if the icon is a base64 string
254260
if (icon.find("data:image") != std::string::npos) {
@@ -258,8 +264,9 @@ - (void)menuDidClose:(NSMenu *)menu {
258264
std::string base64Icon = icon.substr(pos + 7);
259265

260266
// Convert base64 to NSData
267+
NSString* base64String = [NSString stringWithUTF8String:base64Icon.c_str()];
261268
NSData* imageData = [[NSData alloc]
262-
initWithBase64EncodedString:[NSString stringWithUTF8String:base64Icon.c_str()]
269+
initWithBase64EncodedString:base64String
263270
options:NSDataBase64DecodingIgnoreUnknownCharacters];
264271

265272
if (imageData) {
@@ -281,6 +288,9 @@ - (void)menuDidClose:(NSMenu *)menu {
281288
[image setSize:NSMakeSize(16, 16)]; // Standard menu item icon size
282289
[image setTemplate:YES];
283290
[pimpl_->ns_menu_item_ setImage:image];
291+
} else {
292+
// Clear the image if no valid icon is provided
293+
[pimpl_->ns_menu_item_ setImage:nil];
284294
}
285295
}
286296

@@ -449,6 +459,8 @@ - (void)menuDidClose:(NSMenu *)menu {
449459

450460
~Impl() {
451461
if (delegate_) {
462+
// Remove delegate to prevent callbacks after destruction
463+
[ns_menu_ setDelegate:nil];
452464
delegate_ = nil;
453465
}
454466
}
@@ -604,9 +616,13 @@ - (void)menuDidClose:(NSMenu *)menu {
604616
pressure:1.0];
605617

606618
pimpl_->visible_ = true;
607-
// Create a dummy view to avoid the nil warning
608-
NSView* dummyView = [[NSView alloc] init];
609-
[NSMenu popUpContextMenu:pimpl_->ns_menu_ withEvent:event forView:dummyView];
619+
620+
@autoreleasepool {
621+
// Create a dummy view to avoid the nil warning
622+
NSView* dummyView = [[NSView alloc] init];
623+
[NSMenu popUpContextMenu:pimpl_->ns_menu_ withEvent:event forView:dummyView];
624+
}
625+
610626
pimpl_->visible_ = false;
611627

612628
return true;

src/platform/macos/tray_icon_macos.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#import <Cocoa/Cocoa.h>
77
#import <Foundation/Foundation.h>
88

9+
// Note: This file assumes ARC (Automatic Reference Counting) is enabled
10+
// for proper memory management of Objective-C objects.
11+
912
// Forward declarations
1013
@interface TrayIconDelegate : NSObject
1114
@property (nonatomic, assign) nativeapi::TrayIcon* trayIcon;
@@ -37,6 +40,11 @@ - (void)statusItemRightClicked:(id)sender;
3740

3841
~Impl() {
3942
if (ns_status_item_) {
43+
// Remove target and action to prevent callbacks after destruction
44+
if (ns_status_item_.button) {
45+
[ns_status_item_.button setTarget:nil];
46+
[ns_status_item_.button setAction:nil];
47+
}
4048
[[NSStatusBar systemStatusBar] removeStatusItem:ns_status_item_];
4149
ns_status_item_ = nil;
4250
}
@@ -115,6 +123,9 @@ - (void)statusItemRightClicked:(id)sender;
115123

116124
// Set the image to the button
117125
[pimpl_->ns_status_item_.button setImage:image];
126+
} else {
127+
// Clear the image if no valid icon is provided
128+
[pimpl_->ns_status_item_.button setImage:nil];
118129
}
119130
}
120131

0 commit comments

Comments
 (0)