Reputation: 12683
With my knowledge set nil
to a variable when release
it is always a good idea or make no difference. But it is never a bad idea.
So, to set nil to a object we have to do:
[object release];
object=nil;
But this is 2 lines. I always have to remember to do it. So, there are a automatic way to set nil when release a object?
One simple idea is:
#define release(VAR) [VAR release]; VAR=nil;
release(object);
But with this I will have to change the codes in all files and isn't a good idea change the code in this way because it isn't a "natural" way of objective-c. Anyone know another trick?
Upvotes: 0
Views: 131
Reputation: 9198
You almost never need to do this. If you have this throughout your code anywhere but dealloc you are not using accessor methods correctly.
if you do this,
- (void)dealloc {
[foo release];
foo = nil;
[super dealloc];
}
What does the foo = nil;
line get you? The only thing i can think of is that it might hide a bug you would otherwise have found earlier.
So, no, i would not say that it is never a bad thing.
Upvotes: 2
Reputation: 4196
If your object is a retained property:
.h
@property(nonatomic, retain) NSObject *object
.m
@synthesize object;
All you need to do is call:
self.object = nil;
Indeed, the generated setter takes care of doing the release for you, so that one line above is equivalent to writing
[object release];
object=nil;
Upvotes: 2
Reputation: 82209
I reckon you're best off just writing both the lines throughout, as is normal. Then at least your code will make sense to anyone else who has to maintain it later down the line.
Upvotes: 2