Memory Management
Memory Management
- Subject: Memory Management
- From: John MacMullin <email@hidden>
- Date: Sun, 27 Jul 2003 14:37:12 -0700
I have been reading in Cocoa in a Nutshell, at page 9 and 14, the use
of memory management using the following pattern:
interface {
id title;
}
- (id)title {
return title;
}
- (void)setTitle:(id)aTitle {
[title autorelease];
title = [aTitle retain];
}
I have also seen the following method from Objective-C services:
interface {
NSMutableArray *companyRecords;
}
- (NSMutableArray *)companyRecords
{
return companyRecords;
}
- (void)setCompanyRecords:(NSMutableArray *)newCompanyRecords
{
[newCompanyRecords retain];
[companyRecords release];
companyRecords = newCompanyRecords;
}
An finally a third method:
interface {
NSMutableArray *companyRecords;
}
- (NSMutableArray *)companyArray
{
if (!companyRecords) {
companyRecords = [[[NSMutableArray alloc] init] retain];
}
return companyRecords;
}
Question: Is there a preferred method? And does one method cause
problems is used? The release in the last method is set in the dealloc.
John
_______________________________________________
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.