objective-cmacoscocoansworkspacensrunningapplication

NSWorkspace sharedWorkspace runningApplications causing memory leak; alternative option?


I'd like to know whether anyone has a suggestion for an alternative to using runningApplications, as something like the following appears to be leaking memory:

https://openradar.appspot.com/24067155 https://github.com/bradjasper/NSRunningApplicationMemoryLeaks

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkApps:) userInfo:nil repeats:YES];

}

- (void) checkApps : (id) sender {

    @autoreleasepool {

        NSArray *appsArray = [[NSWorkspace sharedWorkspace] runningApplications];

        for (NSRunningApplication *a  in appsArray) {
            NSLog(@"%@", [a localizedName]);
        }

    }

}    

Is the only option to wait until Apple provides a solution? I'm working in a sandboxed environment, so some NSTask-based alternatives may not work. Thanks in advance for any ideas.


Solution

  • The answer to your question, is there another sandboxable option?: is no. This is how you're supposed to look for running applications.

    You might try KVO (on the sharedWorkspace's runningApplications property) instead. The documentation suggests doing just that rather than what you're doing:

    Instead of polling, use key-value observing to be notified of changes to this array property.