• 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: NSArray as a static
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray as a static


  • Subject: Re: NSArray as a static
  • From: "Adam R. Maxwell" <email@hidden>
  • Date: Mon, 01 Sep 2008 14:07:34 -0700


On Sep 1, 2008, at 11:38 AM, Jean-Daniel Dupas wrote:

Unlike java, Obj-C does not have the concept of class variable.
Your static variable is a classic C variable, and C variable are not automatically initialized to NULL.
The first time you call init, relationshipMatch may contains anything and may not be NULL, and so it will never be properly initialized.

Are you sure about that? I believe C static variables are initialized to zero, although I prefer to initialize them explicitly.


Second error, you do not retain you variable. You create it using a convenient initializer wich returns an autoreleased array. At the end of the event loop, your array is released, and as you never retained it, it is deallocated.

Additionally, I'd use +initialize in a case like this, so you're guaranteed that it's initialized before your class is used, and do away with the +initRelationshipMatch method. Using alloc/init avoids the memory management problem that Jean-Daniel pointed out.


+ (void)initialize
{
if (nil == relationshipMatch)
relationshipMatch = [[NSArray allocWithZone:[self zone]] initWithObjects:@"one", @"two", @"three", nil];
}



-- Adam

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

References: 
 >NSArray as a static (From: Richard Good <email@hidden>)
 >Re: NSArray as a static (From: Jean-Daniel Dupas <email@hidden>)

  • Prev by Date: CGEventTap locks up entire system
  • Next by Date: Reflexive Many-to-Many in Core Data?
  • Previous by thread: Re: NSArray as a static
  • Next by thread: Re: NSArray as a static
  • Index(es):
    • Date
    • Thread