简体   繁体   中英

dealloc or releasing best practice in objective-c?

I just simply release objects as such:

[myObj release];

I have seen others add:

[myObj release]; myObj = nil;

Is the latter more advantageous because now there will never be a reference to that pointer?

It is just to make sure that other successive messages that might be send to myObj don't throw an exceptions. Message send to nil are possible in Objective-C.

Otherwise, myObj still points to the memory address where it has been and accessing it might result in a BAD_ACCESS error.

According to me this is purely depend on the scope of myObject.

  1. If myObject scope is getting ended with the release statement then there is no advantage in setting it to nil

  2. If myObject scope don't end with release statement then setting the object to nil make your object access safe. There is good chance that myObject memory is released and you try to access this memory location which will lead to exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM