Re: MOKit memory problem
Re: MOKit memory problem
- Subject: Re: MOKit memory problem
- From: Brock Brandenberg <email@hidden>
- Date: Mon, 12 Aug 2002 09:29:45 -0500
Hi Nico.
MallocDebug is probably correct that the major part of the memory is being
allocated for MORegularExpression, but the memory should be getting freed at
the end of the current run loop. The fact that you're using memory doesn't
necessarily mean that it's not being freed. You're relying on the
autorelease mechanism to free it, so it will happen at the discretion of the
run loop and the default autorelease pool.
In a routine such as this, I'd be more explicit and try to use
retain/release so that the objects are deallocated when you say so, before
you leave the method. This would be difficult by the looks of your if...else
if statements, so I would consider using an autorelease pool inside this
method. If you can use explicit retain/release, it will be a bit faster than
relying on the autorelease mechanism, but it isn't always possible. I'd hate
to try and keep the pointer assignments straight in your case :)
Just create an autorelease pool at the top of your method and release it at
the bottom like this:
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
// do all your work, making sure not to leave the method without first
releasing localPool
[localPool release];
All the temporary objects should be released before leaving the method,
rather than hanging around until the next run loop. You will still show a
lot of memory being used, but not being leaked. There's a big distinction
between these two.
The autorelease pools work like a stack. When you alloc and init one, it is
added to a stack maintained by NSThreqd. Read up on them in the "Memory
Management" section of "Program Design". They can be handy.
>
Here's the code I use :
>
>
- (NSDictionary *)parse:(NSString *)string
>
{
>
NSString *myInfo1, *myInfo2;
>
MORegularExpression *regexp;
>
>
if ([[MORegularExpression regularExpressionWithString:@".* some RE .*?
>
:.*"] matchesString:string])
>
{
>
regexp = [MORegularExpression regularExpressionWithString:@"(.*) some
>
RE (.*?) :(.*)"];
>
myInfo1 = [regexp substringForSubexpressionAtIndex:1
>
inString:string];
>
myInfo2 = [regexp substringForSubexpressionAtIndex:2
>
inString:string];
>
} else if ... // here I verify whether the string matches another regexp
>
{
>
>
} else if ... {
>
// etc..
>
}
>
>
// i return an NSDictionary* created using the strings (myInfo1 and/or
>
myInfo2, and many others)
>
}
>
>
(regexp is used only is the currend method, which is called each time a new
>
data is read from a socket. This method is used to match what kind of info
>
we've just retrieved from an IRC server)
>
>
MallocDebug indicates the major part of the memory is allocated for
>
MORegularExpression.
>
>
Please tell me if I didn't something *WRONG* :]
>
Thanks you :>
>
Nico
Brock Brandenberg
----- industrial design @ www.bergdesign.com ------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.