I have a small AppleScriptObjC app that I use for processing photos. I need to fix a memory leak that I've discovered which seems to be caused by the photos being cached and not being released. This occurs every time I process (initWithContentsOfFile_) which causes the memory usage to increase with each process. The relevant section of my code is:
on applicationWillFinishLaunching_(aNotification)
set inputPath to (choose folder with prompt "Select The Input Directory" default location (path to desktop folder))
tell application "finder"
set imageNames to get name of files of inputPath
end tell
set imagesRemaining to (number of items of imageNames) as string
my errorAlert(imagesRemaining & " photos remaining.")
if (number of items of imageNames) = 0
my errorAlert("There are no items in the directory.")
else
set numberOfImages to number of items of imageNames
set currentPath to inputPath & (item 1 of imageNames) as string
set theImage to initWithContentsOfFile_(POSIX path of currentPath) of alloc() of class "NSImage" of current application
setImage_(theImage) of imageView
set theImage to missing value
end if
end applicationWillFinishLaunching_
This part of the script assigns the first photo, the other processing functions are pretty much identical.
I'm either trying to figure out how to correctly release each photo or, alternatively, use the setCacheMode:NSImageCacheNever to disable the caching function. Despite trying for a long time to figure out the proper syntax in ApplescriptObjC, I can't seem to fix the memory leak. Thanks!
For reference, here's the button action which causes the memory to increase every time executed:
on photoOutput1_(sender)
if output1 = ""
set output1 to (choose folder with prompt "Select The Output Path #1" default location (path to desktop folder))
else
end if
tell application "Finder"
set ProcessedPhoto to quoted form of (POSIX path of CurrentPath)
set photoNumber to photoNumber + 1
set DateAppend to do shell script "date +%m-%d_%H.%M"
set DateAppend to DateAppend & "_" & photoNumber
set OutputPath to quoted form of ((POSIX path of output1) & DateAppend & ".jpg")
try
do shell script "mv -n " & ProcessedPhoto & " " & OutputPath
on error
my errorAlert("Unable to move the photo. Please try again.")
return
end try
set imageNames to get name of files of inputPath
end tell
set imagesRemaining to (number of items of imageNames) as string
my errorAlert(imagesRemaining & " photos remaining.")
if (number of items of imageNames) = 0
imageView's setImage_(missing value)
my errorAlert("There are no more photos in the directory.")
else
set numberOfImages to number of items of imageNames
set currentPath to inputPath & (item 1 of imageNames) as string
set theImage to missing value
imageView's setImage_(theImage)
set theImage to initWithContentsOfFile_(POSIX path of currentPath) of alloc() of class "NSImage" of current application
setImage_(theImage) of imageView
tell application "Finder"
set OutputImageNames to number of files in folder output1
end tell
buttonA's setTitle_("A (" & OutputImageNames & ")")
end if
end photoOutput1_