Re: Memory leaks when running an apple event
Re: Memory leaks when running an apple event
- Subject: Re: Memory leaks when running an apple event
- From: Mike Abdullah <email@hidden>
- Date: Mon, 15 Oct 2007 12:58:03 +0100
There is nothing wrong with this code that I can see; you are
fulfilling your side of the memory management rules. Do NOT release
result (as you found out it will crash). Just accept that
NSAppleScript is doing its own caching internally and may hang on to a
small quantity of memory.
Maybe I missed an earlier part of the conversation, but I don't even
see why this necessitates the use of NSAppleScript; NSWorkspace is
capable of doing the exact same thing and much, much faster.
Mike.
On 15 Oct 2007, at 11:13, Alex Bumbu wrote:
---------- Forwarded message ----------
From: Alex Bumbu <email@hidden>
Date: Oct 15, 2007 1:09 PM
Subject: Re: Memory leaks when running an apple event
To: Rudi Sherry <email@hidden>
Thanx for the reply. I've made the modifications u've recomended to
me but
the problem still exists. I've found that the NSAppleEventDescriptor
*result
consumes the memory, but if i try to release it the application will
crash.
I've tested with this example from apple
http://developer.apple.com/technotes/tn2006/tn2084.html but i'm
having the
same problem.
Here is the code i'm using now:
-(void)isAppRunning
{
NSDictionary* errorInfo;
NSAppleEventDescriptor* result;
NSString* appleEventString = [NSString stringWithFormat:@"set
itunes_active to false\n with timeout of 1 second\n tell application
\"Finder\"\n if (get name of every process) contains \"iTunes\" then
set
itunes_active to true\n end tell\n end timeout\n return
itunes_active"];
NSAppleScript *script = [[NSAppleScript alloc]
initWithSource:appleEventString];
result = [script executeAndReturnError:&errorInfo];
NSLog(@"%@", [result stringValue]);
[script release];
}
-(void)mainProcess:(id)anObject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (1)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self isAppRunning];
[pool release];
}
[pool release];
}
On 10/12/07, Rudi Sherry <email@hidden > wrote:
On Fri, 12 Oct 2007 11:38:58 -0400, Paul Bruneau wrote:
On Oct 12, 2007, at 11:13 AM, Alex Bumbu wrote:
Hi all,
I'm trying to create an simple application using Apple Events to
see if a specified application is running (iTunes in my case). A
thread from main.m will use the mainProcess method so the script
will run every second. The problem I'm having is that the
NSAppleEventDescriptor i'm using (result) isn't deallocated and the
every 100 secs it grows with 1Mb of memory. Can someone help me
please ... I've tried release, dealloc, autorelease and isn't
working. Please if you know give me some ideas how to solve this
problem in cocoa. Thanx guys.
This is the code I'm using:
-(void)isAppRunning
{
NSString* appleEventString = [NSString stringWithFormat:@"set
itunes_active to false\n with timeout of 1 second\n tell
application \"Finder\"\n if (get name of every process) contains
\"iTunes\" then set itunes_active to true\n end tell\n end timeout
\n return itunes_active"];
NSAppleScript *script = [[NSAppleScript alloc]
initWithSource:appleEventString];
NSDictionary *errorInfo;
NSAppleEventDescriptor* result;
result = [script executeAndReturnError:errorInfo];
NSLog(@"%@", [result stringValue]);
[result dealloc];
[script dealloc];
}
-(void)mainProcess:(id)anObject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (1)
{
[self isAppRunning];
}
[pool release];
}
You shouldn't be calling dealloc yourself. That is up to Cocoa to
deal with. You should review the memory management guide several
times (from my experience it takes awhile to start to understand.
http://developer.apple.com/documentation/Cocoa/Conceptual/
MemoryMgmt/
index.html
Two things:
(1) [script release] is what you want to call, as following Paul's
suggesting will reveal.
(2) It's also possible that Mac OS X, when running the script,
retains and autoreleases quite a bit of memory; this won't get freed
until the surrounding pool gets released; note that your autorelase
pool doesn't actually get released until the loop (which runs
forever) returns. Put another allocation and release of an
autorelease pool inside the "while (1)" loop, around the call to to
free this stuff.
Rudi
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden