I have been experimenting with static analysis on my machine, and I have the impression that although it is a great piece of work, it doesn't always find the correct errors.
For example, I have this code:
The code searches for an available name of type "soundXX.wav" (where XX is a 2 digit number) on the file system, and when it finds it, it returns it. Static analyzer rings the bell on this one, and complains that there is a possible memory leak on "nameToTest".
Maybe this is my own fault, but I fail to see the memory leak here. I allocated nameToTest, retained it, released it (since I release the Autorelease pool) and returned it from my function. Shouldn't it be autoreleased afterwards by cocoa since it is an autoreleased object?
I would really like someone to explain this to me, as this may save me a lot of trouble in the future.
For example, I have this code:
Code:
- (NSString *)generateSoundID
{
NSString *result = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSString *soundsDirectory = [[SFGlobals sharedSFGlobals]applicationSoundsFolder];
unsigned i = 0;
BOOL shouldContinueLooping = YES;
do {
NSString *nameToTest = [NSString stringWithFormat:@"%@%u.%@", SOUND_FILE_PREFIX, i, SOUND_FILE_SUFFIX];
NSString *locationToTest = [soundsDirectory stringByAppendingPathComponent:nameToTest];
NSLog(@"now checking: %@", locationToTest);
if (![fm fileExistsAtPath:locationToTest]){
result = [nameToTest retain];
shouldContinueLooping = NO;
}
i++;
} while (shouldContinueLooping == YES);
[pool release];
return result;
}
The code searches for an available name of type "soundXX.wav" (where XX is a 2 digit number) on the file system, and when it finds it, it returns it. Static analyzer rings the bell on this one, and complains that there is a possible memory leak on "nameToTest".
Maybe this is my own fault, but I fail to see the memory leak here. I allocated nameToTest, retained it, released it (since I release the Autorelease pool) and returned it from my function. Shouldn't it be autoreleased afterwards by cocoa since it is an autoreleased object?
I would really like someone to explain this to me, as this may save me a lot of trouble in the future.