Re: memory mgmt for NSMutableArray as ivar
Re: memory mgmt for NSMutableArray as ivar
- Subject: Re: memory mgmt for NSMutableArray as ivar
- From: Andrew Merenbach <email@hidden>
- Date: Tue, 21 Aug 2007 21:42:26 -0700
Hi, Daniel,
The code produced by Accessorizer in this case is inadequate. First,
the corrections:
You'll need a -dealloc method that releases the list variable.
- (void)dealloc {
[self setList:nil];
[super dealloc];
}
Next, your -setList: method needs a conditional test (this is one way
of doing things, and--based upon a query that I made to this list
before--the fastest way):
- (void)setList:(NSArray *)anArray {
if (anArray != list) {
[list release];
list = [anArray copy];
}
}
You need the conditional because, if anArray is the same object as
list when -setList is called, your list variable will be released,
obliterating both list and, therefore, anArray--and that would be
bad, since you want to save the value of anArray.
On to your question: the array class is just like any other
collection class in Cocoa. You'll need an alloc or convenience
constructor of some sort when you initialize the list variable; for
example:
- (id)initWithArray:(NSMutableArray *)anArray {
if (self = [super init]) {
[self setList:anArray];
}
return self;
}
Or:
- (id)init {
if (self = [super init]) {
NSArray *array = [NSArray arrayWithObjects:@"hello", @"mary",
@"lou", nil];
[self setList: array];
}
return self;
}
In some cases, you may just wish to alloc the array yoruself--for
instance, although the -init code above is very clear, you may wish
to do:
- (id)init {
if (self = [super init]) {
list = [[NSArray alloc] initWithObjects:@"hello", @"mary", @"lou",
nil];
}
return self;
}
Since -setList: retains the array, it's the same end-result in either
case.
Finally, your real question: <#....#> is a placeholder for a
variable. In this case:
[self setList: <#(NSMutableArray *)theList#>];
Means that "<#(NSMutableArray *)theList#>" should be replaced with a
variable of type NSMutableArray.
Cheers,
Andrew
On Aug 21, 2007, at 9:21 PM, Daniel Child wrote:
Hi all,
I am trying to set up a class where one of the instance variables
is an NSMutableArray, and want to deal with the memory issues. I've
looked at several of the recommended readings on memory management,
but cant seem to find an example where the instance variable is an
array.
Specifically, does the array need to be somehow allocated and
initialized during the init method used when creating class
instances? If so, how?
I tried Accessorizer, and it produced the following code for a
basic class with an array instance variable:
(in header file)
@interface MyClass : NSObject {
NSMutableArray *list;
}
@end:
(in implementation file)
// init
- (id)init
{
if (self = [super init])
{
[self setList: <#(NSMutableArray *)theList#>];
}
return self;
}
// list ACCESSORS
- (NSMutableArray *) list
{
return [[list retain] autorelease];
}
- (void) setList: (NSMutableArray *) theList
{
[list release];
list = [theList copy];
}
This compiled fine, but I have no idea what the <#....#> part
means, and have not encountered this in the literature. Can someone
explain how this works? Or an alternate safe approach.
Thanks!
Daniel
_______________________________________________
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
_______________________________________________
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