Re: Simple memory problem
Re: Simple memory problem
- Subject: Re: Simple memory problem
- From: harry greenmonster <email@hidden>
- Date: Fri, 6 Feb 2009 00:17:56 +0000
I had already tried an NSAutoreleasePool but came across issues, it
released inputString so as useless for the next iteration. So I tried
keeping it around with...
if (inputString != nil) {
[inputString retain]; /* Keep match around. */
}
[inputString release];
within the NSAutoreleasePool ...and then I'm back to square one.
I understand the release retain concept reasonably well, it's just
that as inputString was being replaced by a pointer of the same name I
presumed the old one was autoreleased OR just replaced the address
location.
So, how do I keep a copy hanging around AND kill the mysterious new
copy then (which shares the same name as the old one presumably)?
Not really; the pointer to the old address is still hanging around
out there until you ensure that it's taken care of. In this case,
yes, the stringByReplacing... method *does* return an autoreleased
object, which will *eventually* get cleaned up, but since you're
doing this in a tight loop, all that eventual cleanup is not going
to be done until (possibly) much later. If you wrap the contents of
your while loop with
NSAutoreleasePool *pool = [NSAutoReleasePool new];
...
[pool drain];
This will clean up the autoreleased inputString each time, instead
of collecting them all for later disposal.
See the Cocoa and Objective-C docs on Memory Management.
_______________________________________________
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