开发者

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];
}

}

开发者_JAVA技巧

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...


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]; .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜