UIAlertView fails to show and results in “EXC_BAD_ACCESS” error

前端 未结 6 2043
一向
一向 2021-01-21 01:05

A method is called when a return button on the keyboard is pressed. After calling another method which returns an integer a message is created based on that integer. The message

相关标签:
6条回答
  • 2021-01-21 01:21

    Try it with NSZombieEnabled = YES.

    • Go into the Info of you Executable.
    • Click on the Arguments tab.
    • Click + on "Variables to be set in the environment."
    • Type NSZombieEnable and YES.

    When the memory is released that has already been released, NSZombie will display the address, then you can use Instruments to find the actual object. Corbin's Treehouse has a good overview of how to do this: Instruments on Leopard: How to debug those random crashes in your Cocoa app

    0 讨论(0)
  • 2021-01-21 01:24

    Objects returned from convenience constructors are already set to autorelease. While you declared a pointer to "message", the "message" object itself doesn't belong to you, since you used the @"string" convenience constructor to create the NSString object. Thus, you don't need to release it.

    When you release it manually, it then gets released too many times (once manually, and once when the autorelease process rolls around) and throws the error.

    Here's some additional information from Apple:

    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html

    Good rule of thumb: unless you use one of the alloc or init or copy methods to create an object (or if you retain the object yourself) you don't need to release it, but can rely on the method that actually created it to do that work for you.

    0 讨论(0)
  • 2021-01-21 01:36

    Sean is right - you don't need to call [message release] here, because you're never actually retaining the message object.

    Instead of just saying message = @"string", you need to say message = [NSString stringWithString:@"string"]; To be completely honest I'm not sure why (maybe someone can comment and I can improve this post!) but that should do the trick.

    0 讨论(0)
  • 2021-01-21 01:37

    This could case due to updating UIKit from background thread

    I solved it like this

    UIAlertView *alertMSG = [[UIAlertView alloc] initWithTitle:nil
                            message:@"Your mnessage here"
                            delegate:self
                            cancelButtonTitle:@"Title here"
                            otherButtonTitles: nil];
    
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
             [alertMSG show];
        }];
    
    0 讨论(0)
  • 2021-01-21 01:40

    I had the same problem here with a UIAlertView, in my case, I had another class implementing the alert, and, from another one I was calling a static method. Like the following:

    ClassA
    
    ...
    
    doSomething {
     ... some stuff ...
    
     [MyAlertView showAlert];
    
     ... some other stuff...
    
    }
    

    What I suspect is that, as the alertview is shown asynchronously when I clicked the button the object was already released.

    To verify that, I changed the code to instantiate the alert and not release it. And everythong worked.

    My final solution was to declare a variable in the parent view, and deallocate it with the other variables when the view is deallocated.

    0 讨论(0)
  • 2021-01-21 01:47

    i had an issue like this ...i was calling uiAlertView from a background thread ....call it from the main thread

    0 讨论(0)
提交回复
热议问题