Memory leak in previous post! Aaaarrgghhh! |
August 2005 |
What have I done?
The entry Calling Applescript from Objective-C and Cocoa." from the 23 of July contains a memory leak published code.
Since I am very new to Cocoa and Objective-C I love to hear about corrections to my code, it's a very fast way of learning!
This memory leak was reported by Guy, via the cocoa-dev lists at Apple:
Hey Jeroen,
Thanks for the code pointer, I'm going to nick that because it's
handy to have around. Less handy are the two memory leaks though. :)
scriptError and appleScript both leak - they're sent allocs but have
no matching release or autorelease. Also, even when you add a release
for the scriptError it'll leak anyway. :) executeAndReturnError: will
overwrite the pointer to the dictionary with the dictionary it creates
internally. It's not a very common idiom in Cocoa ( although there are
a few places that do use it ) but it's something to watch out for.
Here's the adjusted code below. Thanks for putting it up - the
image color automater thing is a very cool idea by the way. Try adding
face recognition and it'd be super-duper cool. :)
Take care,
Guy
A corrected version by Guy follows:
-(void)setCommentForFile:(NSString*)file
toComment:(NSString*)comment
{
NSDictionary *scriptError = nil;
NSString *scriptSource = [NSString stringWithFormat:
@"on run\r tell application \"Finder\"\r set theFile to
(\"%@\") as alias\r set newComment to (comment of theFile) &
\"%@\"\r set comment of theFile to newComment\r end tell\r end run\r",
[self carbonPathFromPath:file], comment];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:scriptSource];
if(![appleScript executeAndReturnError:&scriptError])
NSLog([scriptError description]);
[appleScript release];
}
I did do a bit of "research" recently into Cocoa memory usage and leak detection...
About the face recognition.... uh, no, not anytime soon! ;)
|