NSAppleScript内存泄漏

时间:2014-07-25 10:46:11

标签: objective-c applescript nsautoreleasepool nsapplescript

我有OS X 10.6 SDK的代码。有一点我无法理解。

[1]

int main(int argc, const char*argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *strScript =
    @"set theURL to \"\" \n"
    "tell application \"Safari\" \n"
    "set theURL to URL of current tab of window 1 \n"
    "end tell \n"
    "return theURL \n";

    int poolCount = 0;
    while(1)
    {
        poolCount++;
        NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:strScript] autorelease];
        NSArray *array = [[[script executeAndReturnError:nil] stringValue] componentsSeparatedByString:@" "];

        if (array) {
            NSLog(@"%@", array);
        }

        if (poolCount>=100)
        {
            [pool release];
            pool = [[NSAutoreleasePool alloc] init];
            poolCount = 0;
        }
    }
    [pool release];
}

在代码上,当poolCount为100时,释放并分配autoreleasepool。 我发现那里总是存在泄漏。

但是下面的代码没有泄漏。

[2]

int main(int argc, const char*argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *strScript =
    @"set theURL to \"\" \n"
    "tell application \"Safari\" \n"
    "set theURL to URL of current tab of window 1 \n"
    "end tell \n"
    "return theURL \n";

    while(1)
    {
        NSAutoreleasePool *lpool = [[NSAutoreleasePool alloc] init];

        NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:strScript] autorelease];
        NSArray *array = [[[script executeAndReturnError:nil] stringValue] componentsSeparatedByString:@" "];

        if (array) {
            NSLog(@"%@", array);
        }

        [lpool release];            
    }

    [pool release];
}

内循环的自动释放池是[1]和[2]之间的差异。

请告诉我[1]的内存泄漏原因。

0 个答案:

没有答案