Skip to content

Commit 190c4e7

Browse files
committed
refactor(MenuView): replace NSMutableSet with NSHashTable for disabled views
Use NSHashTable with weak references to prevent memory leaks when views are deallocated. Add safety checks during iteration to handle weak references properly.
1 parent 15cac4d commit 190c4e7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

ios/MenuView.mm

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ @implementation MenuView {
2020
UIColor *_checkedColor;
2121
UIColor *_uncheckedColor;
2222
BOOL _isChildViewButton;
23-
NSMutableSet<UIView *> *_disabledViews;
23+
NSHashTable<UIView *> *_disabledViews;
2424
}
2525

2626
+ (ComponentDescriptorProvider)componentDescriptorProvider
@@ -33,7 +33,7 @@ - (instancetype)initWithFrame:(CGRect)frame
3333
if (self = [super initWithFrame:frame]) {
3434
static const auto defaultProps = std::make_shared<const MenuViewProps>();
3535
_props = defaultProps;
36-
_disabledViews = [[NSMutableSet alloc] init];
36+
_disabledViews = [NSHashTable weakObjectsHashTable];
3737
_isChildViewButton = NO;
3838
}
3939

@@ -121,9 +121,13 @@ - (void)disableUserInteractionRecursively:(UIView *)view
121121

122122
- (void)restoreUserInteractionForDisabledViews
123123
{
124-
for (UIView *view in _disabledViews) {
125-
// Only restore if the view is still in the view hierarchy
126-
if (view.superview != nil) {
124+
// Create a copy of the objects to iterate over since NSHashTable with weak references
125+
// can have objects deallocated during iteration
126+
NSArray<UIView *> *viewsToRestore = [_disabledViews allObjects];
127+
128+
for (UIView *view in viewsToRestore) {
129+
// The view might have been deallocated (weak reference), so check if it's still valid
130+
if (view && view.superview != nil) {
127131
view.userInteractionEnabled = YES;
128132
}
129133
}

0 commit comments

Comments
 (0)