• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Yet another memory management question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Yet another memory management question


  • Subject: Re: Yet another memory management question
  • From: WT <email@hidden>
  • Date: Sun, 5 Jul 2009 06:40:12 +0200

On Jul 5, 2009, at 6:10 AM, mmalc Crawford wrote:

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];
}

There is plenty of Apple sanctioned code that does not follow that recommendation, both in sample code and in documentation.


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.

I think my explanation was clear enough. If you have lots of objects to release in both viewDidUnload and dealloc, the chances of forgetting to do so in both are non-negligible. Refactoring the common release calls into a separate method puts them all in only one place.


- (void)dealloc
{
  [self dispose];
  // deallocation of stuff that was not unloaded
  [otherStuff release];
}


This is missing: [super dealloc]; as the final statement;

Indeed. That's the problem with copy/paste and doing things in Mail, rather than from real code, ie, sometimes we all make silly mistakes. Not an earth-shattering mistake to make in this list, though, since you get a warning when trying to compile without the call to super.


- (void)dispose
{
[myObject release];
myObject = nil;
}


Again, you should use accessor methods rather than direct variable manipulation.

Again, there is plenty of Apple sanctioned code that does not follow that recommendation.


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.

I think everyone understood what I meant. Sometimes I mess up because of genuine confusion on my part and sometimes I mess up because of multitasking. This time it was the latter.


Wagner
_______________________________________________

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


  • Follow-Ups:
    • Re: Yet another memory management question
      • From: mmalc Crawford <email@hidden>
References: 
 >Yet another memory management question (From: DKJ <email@hidden>)
 >Re: Yet another memory management question (From: WT <email@hidden>)
 >Re: Yet another memory management question (From: mmalc Crawford <email@hidden>)

  • Prev by Date: Re: Yet another memory management question
  • Next by Date: Re: Yet another memory management question
  • Previous by thread: Re: Yet another memory management question
  • Next by thread: Re: Yet another memory management question
  • Index(es):
    • Date
    • Thread