Reputation: 55
I want to know if there is any risk in passing a method to an instance that is NOT allocated? At the moment, the my code seems to work perfectly fine without any warnings.
UIView *testview;
[testview someMethod];
This doesn't cause any problem as far as I can observe but wanted to make sure that this is the case.
Upvotes: 0
Views: 99
Reputation: 42139
Passing a message to nil
is not an error in Objective-C, simply a NOP. However, passing a message to an incorrect address, such as an uninitialized or dangling pointer, is an error. Depending on the context where you define your uninitialized variable it may either be implicity initialized to nil
or then you're just getting lucky with that memory being nil
this time. But for the code to be guaranteed safe, you need to guarantee that it's either nil
or pointing to a valid object. The simple way to do that is to initialize it explicitly: UIView *testview = nil;
.
Upvotes: 5