开发者

objective-c releasing uninitialized class members in dealloc method

开发者 https://www.devze.com 2022-12-31 13:18 出处:网络
Regarding over-releasing. Say I have a instance variable defined in Test.h NSString *mystrin开发者_如何学Gog;

Regarding over-releasing. Say I have a instance variable defined in Test.h

NSString *mystrin开发者_如何学Gog;

In my implementation Test.m I do not initialize the variable mystring anywhere. But I release it in dealloc:

-(void)dealloc {
    [mystring release];
}

Is this now over-released? I've been doing the following in dealloc to avoid any issues, however, is this really necessary?

-(void)dealloc {
     if (mystring) [mystring release];
}

It seems that [nil release] shouldn't do anything, can someone verify this with class members?


There is no way to over-release something that never existed in the first place.

Instance variables are initialized to nil and, thus, [mystring release] is messaging nil which is just fine in Objective-C.

Your -dealloc methods do need to call [super dealloc] at the end, though.


First, why are you creating a variable at the class level that you're not initializing anywhere?

As this post is tagged iphone and it's IMPORTANT to manage your memory in the iphone environment it's always a good idea to release something if you've defined even if you don't assign it.

You can call release on an uninitialized variable without any problems, and be sure to [super dealloc]

0

精彩评论

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

关注公众号