Re: Memory Leak with NSMutable String
Re: Memory Leak with NSMutable String
- Subject: Re: Memory Leak with NSMutable String
- From: Chris Hanson <email@hidden>
- Date: Mon, 26 Jan 2004 19:10:49 -0600
On Jan 26, 2004, at 8:09 AM, Alexander F. Hartner wrote:
Thanks for your help. Does this mean that the memory used by these
NSMutableString will only be released once the NSAutoreleasePool is
released.
Yes, because you created them using +stringWithCapacity:.
Currently the memory usage seems to increase even though I am only
using one instance of NSMutableString.
You aren't creating one instance of NSMutableString, you're creating
one million instances of NSMutableString, once per iteration of your
loop.
This comes down to the distinction between variables and the objects
they refer to. The variable is not the object, it's just a pointer to
the object.
Would this problem go away if instead of initialising the
NSMutuableString with the initWith method I would rather call init and
then release it manually rather then relying on auto release ?
First of all, it isn't a problem, it's expected behavior.
Secondly, if you allocate the string with +alloc, initialize it with
-initWithCapacity:, and then release it immediately, it will be
disposed of immediately. Read some of the documentation -- and take
the time to understand it -- to see why this is the expected behavior.
-- Chris
--
Chris Hanson <email@hidden>
bDistributed.com, Inc.
Outsourcing Vendor Evaluation
Custom Mac OS X Development
Cocoa Developer Training
_______________________________________________
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.