• 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: Do I need to release static "class" variables?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Do I need to release static "class" variables?


  • Subject: Re: Do I need to release static "class" variables?
  • From: Daniel Jalkut <email@hidden>
  • Date: Mon, 15 May 2006 23:25:27 -0400

Hi Eric:

On May 15, 2006, at 11:13 PM, Eric wrote:

I know how to declare class variable (as static variable inside the .m
file), however, I'm not sure whether I should (or how) to deallocate class
variable. Please advice.

In general you shouldn't worry about them. Think of them as instance variables on the object that is the class itself. The only reasonable time to deallocate them would be just before the application quits, and that's when the memory is about to be recycled anyway.


Exceptions to this rule are on a case-by-case basis. For instance, if you've got some fleeting use for a class variable that causes it to take a lot of memory up and then no longer be useful, it might be worth manually releasing it so you can get the memory back. Another case is if you rely on side-effects of the object being dealloc'd, such as opened files being closed or network negotiations being finalized.

In those special types of cases I'd recommend simply adding a class method to explicitly "clean up." Then you can call the clean up method from your application's delegate just before quitting (or whenever else makes sense).

Also, if my class variable is not initialized at declaration, should I
initialize them in the +initialize: method?

You can do that or else initialize them just before use. For instance, often a class variable is used to implement a singleton type behavior, where the class variable is only necessary after the first time some code asks for a copy of the singleton. It can be more efficient to avoid allocating the class variable's object until and unless you know you need it.


Daniel

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Do I need to release static "class" variables? (From: Eric <email@hidden>)

  • Prev by Date: What's so special about NSString literals? (RE: memory management)
  • Next by Date: Re: incompatible pointer type warning, but everything OK?
  • Previous by thread: Do I need to release static "class" variables?
  • Next by thread: What's so special about NSString literals? (RE: memory management)
  • Index(es):
    • Date
    • Thread