Re: NSMutableArray morphs itself into an NSArray?
Re: NSMutableArray morphs itself into an NSArray?
- Subject: Re: NSMutableArray morphs itself into an NSArray?
- From: Dustin Voss <email@hidden>
- Date: Mon, 8 Mar 2004 10:37:16 -0800
On 8 Mar, 2004, at 6:30 AM, Jerry Krinock wrote:
In my application, in a subclassed NSWindowController, I declare the
following member object:
NSMutableArray* crontabLines;
During my subclassed -initWindowWithNibName, I alloc/init it:
crontabLines = [NSMutableArray array] ;
[crontabLines retain] ;
Later, in a member method, I use it to temporarily store several
NSString*s,
and then retain it.
I don't know why you can't remove objects, but if you are retaining
crontabLines several times, I do think you are mis-managing memory.
The convenience constructors like +array are intended for local
variables, not instance variables. To allocate an instance variable,
the pattern is:
crontabLines = [[NSMutableArray alloc] init];
If you use +array then the mutable array will be put in the
auto-release pool. It won't get auto-released -- because you retain it
-- but there is no need for it to go in the auto-release pool at all.
Further, once you have created the array in your
-initWindowWithNibName: method, there is no reason to retain it again
in another method of the same class.
Retain is used to keep an object around when it might otherwise be
deallocated. Within your window controller, you don't need to worry
about sudden deallocation, because you had guaranteed a reference count
of at least one in your -initWindowWithNibName: method. You will only
remove that guarantee in your -dealloc method, when you call
[crontabLines release].
You may want to reread the material on memory management at
<
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
index.html>.
_______________________________________________
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.