Утечка памяти в 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];
}
В коде autoreleasepool освобождается и распределяется, когда poolCount равен 100. Я обнаружил, что всегда была утечка.
Но код ниже не имеет утечки.
[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].