Skip navigation.
 
mlRe: Garbage collector vs variable lifetime
FROM : Charles Srstka
DATE : Mon Jun 09 18:37:35 2008

On Jun 9, 2008, at 2:56 AM, John Engelhart wrote:

> Well, in the case of your example, you have a bug: You have 
> statically typed the class to NSArray, not your subclass.  If one 
> applies the 'attribute only applies to the class it was specified 
> for' rule:
>
> By statically typing the class to NSArray, you have certified to the 
> compiler that no other object type will be received as an argument. 
> When you passed it an object of a different type, even a subclass, 
> you broke your promise to the compiler.


What on earth are you talking about? When you're working with 
NSArrays, you're *always* dealing with a subclass. You will *never* 
have an object that's just of class NSArray, because NSArray is a 
class cluster. When you initialize an NSArray, Foundation gives you 
whatever subclass of NSArray it figures is most suitable for whatever 
you're doing. This is even mentioned in the documentation for NSArray.

Let me show you an example:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSArray *array = [NSArray arrayWithObjects:@"foo", @"bar", nil];

    NSLog(@"array is an %@", NSStringFromClass([array class]));

    [pool drain];
    return 0;
}

When run, this gives:

[Session started at 2008-06-09 11:22:30 -0500.]
2008-06-09 11:22:30.641 test[14449:10b] array is an NSCFArray

The Debugger has exited with status 0.

As you see, what I have is an instance of NSCFArray (a private 
subclass for which we have no headers), even though I initialized an 
NSArray. This happens with a lot of other Foundation objects too, like 
NSString (get yourself one of those, and you could end up with a 
NSConstantString, an NSCFString, an NSPathStore2, or who knows what 
else. The only guarantee you have is that the object will provide all 
the methods that the abstract superclass promises, but which 
particular subclass you're getting and what's under the hood, you 
can't know. Heck, the situation might even change, so that the NSArray 
or NSString subclass you get might be different in a later version of 
OS X than it is currently in 10.5.3. Class clusters are extremely 
common in the default Cocoa classes, and are a fairly common practice 
in general with Cocoa programming (my app, for one, uses class 
clusters).  So there's certainly no way a compiler should be able to 
tell what particular subclass of NSArray (or perhaps even a custom 
subclass that you defined) it's got just from looking at an NSArray, 
and certainly no one's promised any such thing to the compiler.

Charles

Related mailsAuthorDate
mlGarbage collector vs variable lifetime Quincey Morris Jun 7, 00:23
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 00:48
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 01:18
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 01:24
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 01:27
mlRe: Garbage collector vs variable lifetime Nick Zitzmann Jun 7, 01:30
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 01:31
mlRe: Garbage collector vs variable lifetime Shawn Erickson Jun 7, 01:33
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 01:42
mlRe: Garbage collector vs variable lifetime Clark Cox Jun 7, 01:55
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 02:05
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 02:36
mlRe: Garbage collector vs variable lifetime George Stuart Jun 7, 02:51
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 7, 05:48
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 05:49
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 06:16
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 7, 07:03
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 7, 09:07
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 10:32
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 10:41
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 13:13
mlRe: Garbage collector vs variable lifetime Ken Thomases Jun 7, 13:14
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 13:34
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:29
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:35
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 14:47
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 15:18
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 15:59
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 7, 16:31
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 7, 17:03
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 7, 20:19
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:30
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 7, 20:34
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:35
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 7, 20:45
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 7, 21:07
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 21:43
mlRe: Garbage collector vs variable lifetime Ricky Sharp Jun 7, 21:43
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:24
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:37
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 00:51
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 8, 01:11
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 8, 02:15
mlRe: Garbage collector vs variable lifetime Bill Bumgarner Jun 8, 02:23
mlRe: Garbage collector vs variable lifetime Michael Ash Jun 8, 04:10
mlRe: Garbage collector vs variable lifetime Quincey Morris Jun 8, 08:24
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 09:40
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 09:47
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 8, 10:05
mlRe: Garbage collector vs variable lifetime Paul Sargent Jun 8, 12:23
mlRe: Garbage collector vs variable lifetime Chris Kane Jun 9, 01:53
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 02:39
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 05:48
mlRe: Garbage collector vs variable lifetime Peter Duniho Jun 9, 07:55
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 09:56
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 9, 10:11
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 12:33
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 12:51
mlRe: Garbage collector vs variable lifetime Antonio Nunes Jun 9, 13:54
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 9, 18:37
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 9, 19:24
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 20:02
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 9, 20:17
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 9, 20:39
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 10, 00:55
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 10, 17:19
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 10, 17:28
mlRe: Garbage collector vs variable lifetime Adam R. Maxwell Jun 10, 18:18
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 10, 22:17
mlRe: Garbage collector vs variable lifetime John Engelhart Jun 11, 09:01
mlRe: Garbage collector vs variable lifetime Chris Hanson Jun 11, 09:15
mlRe: Garbage collector vs variable lifetime Jim Puls Jun 11, 09:29
mlRe: Garbage collector vs variable lifetime Graham Cox Jun 11, 09:35
mlRe: Garbage collector vs variable lifetime Jean-Daniel Dupas Jun 11, 09:41
mlRe: Garbage collector vs variable lifetime Hamish Allan Jun 11, 11:06
mlRe: Garbage collector vs variable lifetime Andy Lee Jun 11, 14:42
mlRe: Garbage collector vs variable lifetime Charles Srstka Jun 11, 16:55
mlRe: Garbage collector vs variable lifetime Clark Cox Jun 11, 17:45
mlRe: Garbage collector vs variable lifetime j o a r Jun 11, 17:49
ml[moderator] Re: Garbage collector vs variable lifetime Scott Anguish Jun 11, 17:51