Re: Yet another memory management question
Re: Yet another memory management question
- Subject: Re: Yet another memory management question
- From: mmalc Crawford <email@hidden>
- Date: Sat, 04 Jul 2009 21:10:12 -0700
On Jul 4, 2009, at 8:11 PM, WT wrote:
The following is ok, though, assuming that you have appropriately
declared myObject in your class (for example, as an instance
variable):
- (void)viewDidLoad
{
myObject = [[NSObject alloc] init];
}
In general, this is not recommended.
If you manipulate an instance variable anywhere other than in an
initialiser or a dealloc method, you should use a suitable accessor
method.
- (void)viewDidLoad
{
id anObject = [[NSObject alloc] init];
[self setMyObject:anObject];
[anObject release];
}
Both are fine, but I would suggest something like the following,
just because it avoids code duplication:
- (void)viewDidUnload
{
[self dispose];
}
It is not clear here what is the benefit of "avoiding code
duplication" -- you're simply introducing another method that you have
to keep track of.
- (void)dealloc
{
[self dispose];
// deallocation of stuff that was not unloaded
[otherStuff release];
}
This is missing:
[super dealloc];
as the final statement;
- (void)dispose
{
[myObject release];
myObject = nil;
}
Again, you should use accessor methods rather than direct variable
manipulation.
You can have -dispose be a private method of your class so it won't
be accessible outside of it. One big advantage of this separation is
that if/when you need to change your deallocations, you only have to
do it in one place.
Referring to this as "deallocations" is at best misleading. The goal
is to relinquish ownership of any objects you're holding on to. This
may or may not result in deallocation of those objects.
mmalc
_______________________________________________
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