Zhao Xiang
Zhao Xiang

Reputation: 1643

Objective C: How to tell if an object is NSZombie now

I have NSZombieEnabled=YES set, and I want to do the following code

- (NSString*) udid
{
    if (udid == nil) 
    {
        udid = [[UIDevice currentDevice] uniqueIdentifier];
        NSLog(@"UDID=%@", udid);
    }
    return udid;
}

it turns out when udid is "released", it had been replaced with a Zombie, it's not nil. So I want to do something like

if (udid == nil || [udid isZombie])
{
    udid = [[UIDevice currentDevice] uniqueIdentifier];
    NSLog(@"UDID=%@", udid);
}

I tried [udid isKindOf:[NSZombie Class]], but NSZombie does not exist. So how can I tell is the udid object a Zombie now?

Upvotes: 2

Views: 2411

Answers (2)

justin
justin

Reputation: 104698

any message to a zombie will halt the program.

choose one which should not have side effects, such as self:

[udid self] // program will halt here if it's a zombie

any other approach to debugging or writing a program when zombie hunting seems pointless. that is, zombies do not exist in properly written programs, and they only exist under very specific debug-only conditions.

Upvotes: 4

Caleb
Caleb

Reputation: 125007

A pointer to an object is never set to nil when the object is released. The pointer continues to point to the same memory location that it always did. That doesn't mean that whatever is now at that location is a valid object, however. For that reason, you should never use a pointer after you've released the object it points to. If you're going to continue to use that pointer, you should change its value so that it's either nil or points to some other (valid) object.

NSZombieEnabled is just a debugging tool that helps you find places in your code where you're accessing invalid objects. If you've found a place where you're doing that, you've found a bug in your code and you need to fix it, not tolerate it.

Change your code so that you properly set your udid pointer to nil once you've released it.

Upvotes: 3

Related Questions