UIAlertView crashing on undocumented method

后端 未结 1 752
野趣味
野趣味 2020-12-14 13:25

Our app has been crashing with a frequency of roughly 1 in 1,500 launches due to a bug that is proving elusive. The relevant portion of the stack trace is included. It\'s

相关标签:
1条回答
  • 2020-12-14 13:28

    It's likely that UIAlertView is trying to call a method on its delegate after that delegate has been released. To prevent this type of bug, any time you set an object as another object's delegate, set the delegate property to nil in the delegate object's dealloc method. e.g.

    
    @implementation YourViewController
    @synthesize yourAlertView;
    
    - (void)dealloc {
        yourAlertView.delegate = nil; // Ensures subsequent delegate method calls won't crash
        self.yourAlertView = nil; // Releases if @property (retain)
        [super dealloc];
    }
    
    - (IBAction)someAction {
        self.yourAlertView = [[[UIAlertView alloc] initWithTitle:@"Pushed"
                             message:@"You pushed a button"
                             delegate:self
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil] autorelease];
        [self.yourAlertView show];
    }
    
    // ...
    
    @end
    
    0 讨论(0)
提交回复
热议问题