Exegesis
Exegesis

Reputation: 1088

Obj-c IOS Memory Management

Question #1: As a rule, I never release an object if I don't have to. Assuming that stringWithUTF8String has an autorelease inside itself, I don't have to do "return [... autorelease]", right?

-(NSString*)nonNullDBString:(const unsigned char*)value {
if(value == nil) {
    return @"";
} else {
    return [NSString stringWithUTF8String:(char *)value];
}

}

Question #2: In my class I have the attribute: "const uint8_t *bytes;". In the dealloc method must I call "bytes = nil;" or "free(bytes);", or nothing at all?

Question #3: For @property(nonatomic, retain) variables in my classes, what is the best practice of dealloc'ing, is it "self.foo = nil;" or "[foo releease] (what I am doing now)". Additionally, I do not want to mess with KVO issues, whatever they are...

Upvotes: 0

Views: 177

Answers (1)

hoha
hoha

Reputation: 4428

A1) Right. Your snippet is correct.

A2) If you malloc'ed it then call free (it's a C after all). No need to do foo = nil - your object will be dead upon return from dealloc, nobody cares what this pointer value is anymore.

A3) [foo release]; .

Upvotes: 1

Related Questions