Re: memory mgmt for NSMutableArray as ivar
Re: memory mgmt for NSMutableArray as ivar
- Subject: Re: memory mgmt for NSMutableArray as ivar
- From: Kevin Callahan <email@hidden>
- Date: Tue, 21 Aug 2007 22:25:46 -0700
On Aug 21, 2007, at 9:42 PM, Andrew Merenbach wrote:
Hi, Daniel,
The code produced by Accessorizer in this case is inadequate.
For the most part, I think Daniel needs to choose a correct accessor
pair (and init method) from the options in Accessorizer.
In addition to various accessor styles (look at the popup choices --
which include 5 getters (plus 3 more for Core Data) and 12 setters
(plus 4 more for Core Data),
you can turn on dealloc (a few options there, too),
you can turn on a conditional,
you can include an -(id)initWith... method,
you can set a default accessor pair based on type so that
Accessorizer chooses the pair for you automatically,
... you also get support for indexed accessors, keyed archiving,
copyWithZone:, locking, KVO, Singleton generation and appearance/
style customization
a lot of this goes away if/when you go GC, but ...
Kevin
http://www.kevincallahan.org/
http://www.kevincallahan.org/software/accessorizer.html
http://www.xeniamara.com/
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
http://www.kevincallahan.org/
http://www.kevincallahan.org/software/accessorizer.html
http://www.xeniamara.com/Welcome.html
_______________________________________________
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