Re: A quick memory (release) question
Re: A quick memory (release) question
- Subject: Re: A quick memory (release) question
- From: Paul Harvey <email@hidden>
- Date: Wed, 1 Feb 2006 09:47:50 +0000
Yes, thanks Scott, that does make sense. With dictionaryWithCapacity,
you have not used [NSMutableDictionary alloc], so this is a good sign
that it is an autoreleasing setup right?
I've been going over memory management again as I find this whole
subject quite difficult to grasp sometimes.
--
Paul Harvey
Lead Programmer
email@hidden
Hiddenfield Software
"Creating useful software for Mac OS X"
www.hiddenfield.com
On Jan 31, 2006, at 13:58, Scott Thompson wrote:
On Jan 31, 2006, at 5:23 AM, Clark Cox wrote:
On 1/31/06, Paul Harvey <email@hidden> wrote:
Two quick examples:
1 NSMutableAttributedString *ns = [[NSMutableAttributedString
alloc]
initWithString: chapterText];
2 NSString *lookFor = [[NSString alloc]initWithFormat:@"\n%d",i];
The rule for memory management says that if you obtain an object
through alloc, you have retained it and it must be manually released
at a later point.
Yet methods like initWithFormat create an autoreleased object don't
they?
No, they don't. Both of your examples were obtained from alloc,
period. So, you are responsible for releasing them.
Your confusing init methods (which don't generally allocate
objects) with class methods that do allocate objects.
For example, if I call:
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc]
initWithCapacity: 10];
Then I get back an object that I am responsible for releasing
because I called "alloc"
However, if I call:
NSMutableDictionary *myDictionary = [NSMutableDictionary
dictionaryWithCapacity: 10];
Then I get back an object that by the conventions of Cocoa has been
autoreleased.
dictionaryWithCapacity is a class method that returns a new
object. initWithCapacity is an instance method that you call when
an object is new, but it is called on an already allocated object.
Does that make sense?
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden