Skip to content

Commit

Permalink
IOS7: Autorelease overloaded key arrays
Browse files Browse the repository at this point in the history
There are memory leakages when returning to the ScummVM iOS
application if it has been running in background.
The reason for the memory leaks is that the system calls the
keyCommands function every time the keyboard view became the
first responder.

Since the overloaded keys are loaded into allocated arrays
each time the keyCommands function is called they are never
released. Since the keyCommands is running in an autorelease
block, make the allocation to autorelease.

This is required since we don't utilize ARC.
  • Loading branch information
larsamannen committed Aug 8, 2024
1 parent a3f1678 commit 6eac138
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions backends/platform/ios7/ios7_keyboard.mm
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ - (UIKeyCommand *)createKeyCommandForKey:(NSString *)key withModifierFlags:(UIKe
}

- (NSArray *)overloadKeys:(NSArray<NSString *> *)keys withSelector:(SEL)selector {
NSMutableArray<UIKeyCommand *> *overloadedKeys = [[NSMutableArray alloc] init];
NSMutableArray<UIKeyCommand *> *overloadedKeys = [[[NSMutableArray alloc] init] autorelease];
for (NSString *key in keys) {
[overloadedKeys addObject:[self createKeyCommandForKey:key withModifierFlags:0 andSelector:selector]];
[overloadedKeys addObject:[self createKeyCommandForKey:key withModifierFlags:UIKeyModifierShift andSelector:selector]];
Expand All @@ -296,13 +296,13 @@ - (NSArray *)overloadKeys:(NSArray<NSString *> *)keys withSelector:(SEL)selector
}

- (NSArray *)overloadArrowKeys {
NSArray<NSString *> *arrowKeys = [[NSArray alloc] initWithObjects:UIKeyInputUpArrow, UIKeyInputDownArrow, UIKeyInputLeftArrow, UIKeyInputRightArrow, nil];
NSArray<NSString *> *arrowKeys = [[[NSArray alloc] initWithObjects:UIKeyInputUpArrow, UIKeyInputDownArrow, UIKeyInputLeftArrow, UIKeyInputRightArrow, nil] autorelease];
return [self overloadKeys:arrowKeys withSelector:@selector(handleArrowKey:)];
}

- (NSArray *)overloadRomanLetters {
NSString *romanLetters = @"abcdefghijklmnopqrstuvwxyz";
NSMutableArray<NSString *> *letters = [[NSMutableArray alloc] init];
NSMutableArray<NSString *> *letters = [[[NSMutableArray alloc] init] autorelease];
for (NSUInteger x = 0; x < romanLetters.length; x++) {
unichar c = [romanLetters characterAtIndex:x];
[letters addObject:[NSString stringWithCharacters:&c length:1]];
Expand All @@ -312,7 +312,7 @@ - (NSArray *)overloadRomanLetters {

- (NSArray *)overloadNumbers {
NSString *numbers = @"0123456789";
NSMutableArray<NSString *> *numArray = [[NSMutableArray alloc] init];
NSMutableArray<NSString *> *numArray = [[[NSMutableArray alloc] init] autorelease];
for (NSUInteger x = 0; x < numbers.length; x++) {
unichar c = [numbers characterAtIndex:x];
[numArray addObject:[NSString stringWithCharacters:&c length:1]];
Expand All @@ -323,7 +323,7 @@ - (NSArray *)overloadNumbers {
- (NSArray *)overloadFnKeys {
#ifdef __IPHONE_13_4
if (@available(iOS 13.4, *)) {
NSArray<NSString *> *fnKeys = [[NSArray alloc] initWithObjects:UIKeyInputF1, UIKeyInputF2, UIKeyInputF3, UIKeyInputF4, UIKeyInputF5, UIKeyInputF6, UIKeyInputF7, UIKeyInputF8, UIKeyInputF9, UIKeyInputF10, UIKeyInputF11, UIKeyInputF12, nil];
NSArray<NSString *> *fnKeys = [[[NSArray alloc] initWithObjects:UIKeyInputF1, UIKeyInputF2, UIKeyInputF3, UIKeyInputF4, UIKeyInputF5, UIKeyInputF6, UIKeyInputF7, UIKeyInputF8, UIKeyInputF9, UIKeyInputF10, UIKeyInputF11, UIKeyInputF12, nil] autorelease];
return [self overloadKeys:fnKeys withSelector:@selector(handleFnKey:)];
}
#endif
Expand All @@ -332,7 +332,7 @@ - (NSArray *)overloadFnKeys {

- (NSArray *)overloadSpecialKeys {
#ifdef __IPHONE_13_4
NSMutableArray<NSString *> *specialKeys = [[NSMutableArray alloc] initWithObjects:UIKeyInputEscape, UIKeyInputPageUp, UIKeyInputPageDown, nil];
NSMutableArray<NSString *> *specialKeys = [[[NSMutableArray alloc] initWithObjects:UIKeyInputEscape, UIKeyInputPageUp, UIKeyInputPageDown, nil] autorelease];

if (@available(iOS 13.4, *)) {
[specialKeys addObject: UIKeyInputHome];
Expand Down Expand Up @@ -479,7 +479,7 @@ - (void)handleSpecialKey:(UIKeyCommand *)keyCommand {
}

- (NSArray *)keyCommands {
NSMutableArray<UIKeyCommand *> *overloadedKeys = [[NSMutableArray alloc] init];
NSMutableArray<UIKeyCommand *> *overloadedKeys = [[[NSMutableArray alloc] init] autorelease];
// Arrows
[overloadedKeys addObjectsFromArray:[self overloadArrowKeys]];
// Roman letters
Expand Down

0 comments on commit 6eac138

Please sign in to comment.