• 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: observing dealloc
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: observing dealloc


  • Subject: Re: observing dealloc
  • From: Wincent Colaiuta <email@hidden>
  • Date: Tue, 29 May 2007 17:32:40 +0200

El 29/5/2007, a las 16:59, James Bucanek escribió:

Use static variables.

You could have fifty classes that all implement ...

    static id sFoo;     // really stupid name

    ...

    @implementation AnyClass

    + (void)initialize
    {
        // if (sFoo==nil) -- pointless test; initialize only runs once
        sFoot = [[NSObject alloc] init];
    }

Although Apple's docs indicate that the runtime will send the initialize message once per class, that doesn't mean that the test in the above code is pointless. As the example code below demonstrates, the super class initialize method will be called for subclasses if they don't implement it, which means that the same static variable can end up getting used in both cases. Also, even though the docs say that you "typically" don't need to call +[super initialize], there are plenty of examples of code out in the wild which do exactly that and so you may need to be aware of that also if you're writing frameworks or code which may be used by others.


Example:

#import <Foundation/Foundation.h>

@interface Foo : NSObject {}
@end

@implementation Foo
+ (void)initialize
{
static int count = 0;
NSLog(@"+[%@ initialize]: %d", NSStringFromClass([self class]), count++);
}
@end


@interface Bar : Foo {}
@end

@implementation Bar
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [Foo class];
    [Bar class];
    [pool release];
    return 0;
}

Output:

+[Foo initialize]: 0
+[Bar initialize]: 1

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: observing dealloc
      • From: James Bucanek <email@hidden>
    • Re: observing dealloc [Solved]
      • From: Ken Tozier <email@hidden>
References: 
 >Re: observing dealloc (From: James Bucanek <email@hidden>)

  • Prev by Date: Re: observing dealloc
  • Next by Date: memory mgmt in convenience and accessor methods
  • Previous by thread: Re: observing dealloc
  • Next by thread: Re: observing dealloc [Solved]
  • Index(es):
    • Date
    • Thread