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?
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?
It's a long-running debate as to whether setting the pointer to nil after 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 nil won'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.
No, it is not strictly required.
If your code is well-structured there's no need to force myObject to be equal to nil.
However it may be a good habit: release does not immediately destroy the object, just decrease the retain count. Thus it may be,that, even if you call release, the object will still be there for a while, creating problems if you try to send it messages.
Using myObject = nil; eliminate this problem, since, even if you send a message to myObject nothing will actually happen.
It is not required, but mostly a style issue. Assigning nil after the release ensures, that you cannot accidentally use the released reference again (which may or may not result in a crash). Simply calling release on the reference may make the underlying memory go away. However, the pointer will still point to the (now potentially invalid) address, and the result of a subsequent method invocation using that pointer are undefined.
No, it's not required.
It's a safety thing. If you have
[myObject release];
and then somewhere else you do
[myObject doSomething];
Then you will get problems.
If you have
[myObject release];
myObject = nil;
and then somewhere else you do
[myObject doSomething];
Then nothing will happen because you are calling on a nil object. So your app won't just fall over in a big heap. Or if you then have somewhere else in the code
[myObject release];
Then it will release on a nil object and so won't over-release.
Obviously you should just avoid calling on an object that you have already released!
I would always set to nil.
Apple themselves sometimes (documented) check for nil.
Example;
If you set the navigationItem.titleView needs to be set back to nil if you want to revert back to using navigationItem.title, or your title will not be displayed.
There are lots of references to this "nil checking" in the Apple documentation.
Reference;
Restoring navigationItem.title after removing navigationItem.titleView
not really, but better to use it to prevent errors...
if you call somewhere in your code myObject when it's been release it give you a bad error, but if it's stetted to nil the error may by bypassed
if you try:
myObject.someProperty = 1;
or
if (myObject) {...}
and you just released myObject, it just could crash your app...
It is not required, but it is generally considered to be good practice in all environments, with the exception that it is generally considered unnecessary in a -dealloc method.
The reason it is common to set the object pointer to nil after release is because the Objective-C method dispatcher will not try to send a message to a nil object, which means that you're safe to accidentally use the object later.
I would suggest a hybrid approach. Let us look at the typical app requirements:
A good solution is to conditionally assign to:
This brings stability in production builds, but in debug mode any attempt to access the variable will explicitly crash the application. Please note that calling release on an object does not guarantee that the object memory will be deleted hence the need to explicitly set to bad pointer.
#ifdef DEBUG
#define RELEASE(obj) [(obj) release]; (obj) = (id)0x20;
#else
#define RELEASE(obj) [(obj) release]; (obj) = nil;
#endif
after release an abject but not assign NULL, it keeps the address but content is deallocated. So now it is not pointing to any valid memory location.Hence ptr is now dangling pointer which probably having a address but not pointing to any valid memory location. so it is good practice to assign NULL after freeing the allocated memory as below
ptr=NULL;
in this way dangling problem will be solved.
At an Apple Tech Talk I went to a couple of years ago the Apple engineers discussed a few instances where it was essential to assign to nil, but I can't recall the details other than it was inside dealloc in the instances discussed. Suffice it to say that it's not correct to say that you never have to or that it's bad-practice to do so. Sometimes you have to. But most times you don't have to. Sorry my memory isn't clearer.