Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ok to release a pointer thats nil?

If I create a new object that includes two object pointers (see below) when the object is created the pointers are set to point to nil;

@interface rocketShip : NSObject {
    NSString *name; 
    NSNumber *thrust;
}

If (for some unexpected reason) I don't assign these pointers and later release them in my dealloc method is that ok, I am pretty sure it is, just wanted to check?

- (void)dealloc{
    [name release];
    name = nil;
    [thrust release];
    thrust = nil;
    [super dealloc];
}

gary

like image 811
fuzzygoat Avatar asked Oct 01 '09 12:10

fuzzygoat


1 Answers

Sending a message to nil won't cause an error, so this is fine. You need to make sure the pointers are actually nil though - sending a message to a garbage pointer will likely cause errors.

like image 100
Adam Wright Avatar answered Sep 22 '22 18:09

Adam Wright