NSAppleScript memory leak

215 views Asked by At

I have the code with OS X 10.6 SDK. There is one thing I can't understand.

[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];
}

On the code, autoreleasepool is released and alloced when poolCount is 100. I found there always existed a leak.

But the code below doesn't have a leak.

[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];
}

The autoreleasepool for inner loop is the difference between [1] and [2].

Please let me know why memory leaks in case of [1].

0

There are 0 answers