I usually release the object after use by
[myObject release];
But I found in some online tutorials that they assign a nil after releasing the object. Like
[myObject release];
myObject = nil;
Is it required?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a long-running debate as to whether setting the pointer to
nilafter releasing is necessary, but I come down on the side of it being a good idea.After the object is released, the pointer you hold to it still points to the same place. If your release has taken the retain count to 0 then the object will be deallocated. If you then try and send a message to the deallocated object you’ll get an EXC_BAD_ACCESS error. However, sending a message to the pointer after it’s been set to
nilwon’t error – it just won’t do anything.The other side of the argument is that if you’re messaging a deallocated object it’s good to know about it and fix your code to make sure it doesn’t happen.
There are smart people in both camps.