• 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: 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


  • Follow-Ups:
    • Re: Yet another memory management question
      • From: Michael Ash <email@hidden>
    • Re: Yet another memory management question
      • From: Benjamin Dobson <email@hidden>
    • Re: Yet another memory management question
      • From: WT <email@hidden>
    • Re: Yet another memory management question
      • From: DKJ <email@hidden>
References: 
 >Yet another memory management question (From: DKJ <email@hidden>)
 >Re: Yet another memory management question (From: WT <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