泄漏启用和禁用runAtStartupEnabled

时间:2011-03-30 12:46:25

标签: cocoa xcode memory-leaks

我只想问你是否在这里看到一些泄漏:

+ (BOOL)runAtStartupEnabled
{
    NSURL *itemURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    Boolean foundIt=false;
    LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginItems)
    {
        UInt32 seed = 0U;
        NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
        for (id itemObject in currentLoginItems)
        {
            LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;

            UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
            CFURLRef URL = NULL;
            OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
            if (err == noErr)
            {
                foundIt = CFEqual(URL, itemURL);
                CFRelease(URL);

                if (foundIt)
                {
                    break;
                }
            }
        }
        CFRelease(loginItems);
    }
    return (BOOL)foundIt;
}

+ (void)setRunAtStartupEnabled:(BOOL)enabled
{
    NSURL *itemURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    LSSharedFileListItemRef existingItem = NULL;

    LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if(loginItems)
    {
        UInt32 seed = 0U;
        NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
        for (id itemObject in currentLoginItems)
        {
            LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;

            UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
            CFURLRef URL = NULL;
            OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, NULL);
            if (err == noErr)
            {
                Boolean foundIt = CFEqual(URL, itemURL);
                CFRelease(URL);

                if (foundIt)
                {
                    existingItem = item;
                    break;
                }
            }
        }

        if (enabled && (existingItem == NULL))
        {
            LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, (CFURLRef)itemURL, NULL, NULL);

        }
        else if(!enabled && (existingItem != NULL))
        {
            LSSharedFileListItemRemove(loginItems, existingItem);
        }

        CFRelease(loginItems);
    }       
}

0 个答案:

没有答案