I am seeing the condition described in this question but with an important caveat. Consider this code:
@implementation UIAlertView (Factory)
+ (instancetype)alertViewWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
for (NSString *otherButtonTitle in otherButtonTitles)
{
[alertView addButtonWithTitle:otherButtonTitle];
}
return alertView;
}
@end
If I do this:
@property (nonatomic, weak) UIAlertView *alertView;
...
self.alertView = [UIAlertView alertViewWithTitle:@"Hi" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[self.alertView show];
When I build in debug mode it works -- i.e. the factory method returns an autoreleasing pointer, so self.alertView
has a value. Once it's shown, it's retained in the view hierarchy. This is not something I want to do, though -- this was a mistake and it reared its head as an actual problem when I built for release: the compiler (logically) appears to optimize away the assignment, leaving self.alertView
equal to nil
.
Even if I switch the analyzer to run on my release build it is not flagging this use of a weak pointer.
Is there a way to get the compiler / pre-compiler to flag this?