开发者

Objective C: Why is this code leaking?

I'm trying to implement a method similar to what mytunescontroller uses to check if it has been added to the user's login items. This code compiles without warnings but if I run the leaks performance tool I get the following leaks:

Leaked Object  #    Address         Size   Responsible Library  Responsible Frame
NSURL          7    < multiple >    448    LaunchServices       LSSharedFileListItemGetFSRef
NSCFString     6    < multiple >    432    LaunchServices       LSSharedFileListItemGetFSRef

Here is the culprit:

- (BOOL)isAppStartingOnLogin 
{
    LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginListRef) {
        NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginListRef, NULL);
        NSURL *itemURL;
        for (id itemRef in loginItemsArray) {           
            if (LSSharedFileListItemResolve((LSSharedFileListItemRef)itemRef, 0, (CFURLRef *) &itemURL, NULL) == noErr) {
                if ([[itemURL path] hasPrefix:[[NSBundle mainBundle] bundlePath]]) {
                    [loginItemsArray release];
                    [itemURL release];
                    CFRelease(loginListRef);
                    return YES;
                 }
             }
 开发者_如何学运维       }
    [itemURL release];
    [loginItemsArray release];
    CFRelease(loginListRef);

    }

    return NO;
}


LSSharedFileListItemResolve() returns an owned object in the third parameter. This can be verified by reading the header. As a result, you need to release itemURL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜