UIAlertView无法显示并导致“EXC_BAD_ACCESS”错误

时间:2009-07-06 18:37:32

标签: iphone memory-management uialertview exc-bad-access

按下键盘上的返回按钮时会调用方法。在调用另一个返回整数的方法之后,将根据该整数创建一条消息。然后将消息传递到UIAlterView并显示给用户。警报没有任何选项(因此我没有给代表打电话),只是简单地通知用户发生了什么。

编辑:以下是完整方法(之前显示的部分)。当我在UIAlertView之前注释掉所有内容并替换字符串@“test”而不是传递消息时,警报会成功显示。我的结构没有正确处理内存吗?

- (IBAction)joinButton {
    struct userInfo localUser;

    [emailAddress resignFirstResponder];

    //convert textField text to char array in structure
    localUser.firstName = [self convertStringtoCharArray:firstName.text];
    localUser.lastName = [self convertStringtoCharArray:lastName.text];
    localUser.username = [self convertStringtoCharArray:username.text];
    localUser.email = [self convertStringtoCharArray:emailAddress.text];
    localUser.ipAddress = [self convertStringtoCharArray:localIPAddress.text];
    localUser.latitude = currentLocation.coordinate.latitude;
    localUser.longitude = currentLocation.coordinate.longitude;

    //pass structure to be sent over socket
    int result = [myNetworkConnection registerWithServer:&localUser];

    NSString *message = nil;

    //process result of sending attempt
    if (result == 0) {
        //registration succesful
        message = [NSString stringWithString:@"Registration successful"];
    } else if (result == 1) {
        //server unavailable
        message = [NSString stringWithString:@"Server unavailable. Please check your wi-fi settings and try again."];
    } else if (result == 2) {
        //unable to establish connection
        message = [NSString stringWithString:@"Unable to communicate with server. Please check your wi-fi settings and try again."];
    } else if (result == 3) {
        //username already in use
        message = [NSString stringWithString:@"Username in use. Try another username."];
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration"
                                                    message:message
                                                   delegate:nil 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil];

    [alert show];
    [alert release];
}

当我执行代码时,iPhone会变灰,就像它即将显示警报但崩溃一样。我在控制台中收到EXC_BAD_ACCESS错误。我没有正确发布警报或消息吗?这是控制台输出:

Program received signal:  “EXC_BAD_ACCESS”.
(gdb) backtrace
#0  0x30011944 in objc_msgSend ()
#1  0x3054803e in NSPopAutoreleasePool ()
#2  0x3054c808 in -[NSAutoreleasePool release] ()
#3  0x30936ac4 in _UIApplicationHandleEvent ()
#4  0x3204696c in PurpleEventCallback ()
#5  0x30254a76 in CFRunLoopRunSpecific ()
#6  0x3025416a in CFRunLoopRunInMode ()
#7  0x320452a4 in GSEventRunModal ()
#8  0x308f037c in -[UIApplication _run] ()
#9  0x308eea94 in UIApplicationMain ()
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14
(gdb) frame 10
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14 14       int retVal = UIApplicationMain(argc, argv, nil, nil);

修改已移除[message release];,并根据答案使用[NSString stringWithString];分配字符串。

6 个答案:

答案 0 :(得分:8)

我有一个像这样的问题...我从后台线程调用uiAlertView ....从主线程中调用它

答案 1 :(得分:4)

从便捷构造函数返回的对象已设置为自动释放。当您声明指向“message”的指针时,“message”对象本身并不属于您,因为您使用了@“string”便捷构造函数来创建NSString对象。因此,您无需释放它。

当您手动释放它时,它会被释放太多次(一次手动释放,一次自动释放过程滚动)并抛出错误。

以下是Apple提供的其他信息:

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

良好的经验法则:除非您使用alloc或init或copy方法之一来创建对象(或者如果您自己保留对象),则不需要释放它,但可以依赖实际的方法创造它为你做这项工作。

答案 2 :(得分:1)

尝试使用NSZombieEnabled = YES。

  • 进入您的可执行文件信息。
  • 单击Arguments选项卡。
  • 点击+“要在环境中设置的变量。”
  • 输入NSZombieEnable和YES。

当释放已释放的内存时,NSZombie将显示该地址,然后您可以使用Instruments查找实际对象。 Corbin的树屋很好地概述了如何做到这一点: Instruments on Leopard: How to debug those random crashes in your Cocoa app

答案 3 :(得分:0)

Sean是对的 - 你不需要在这里调用[message release],因为你从未真正保留过消息对象。

而不是只说message = @"string",你需要说message = [NSString stringWithString:@"string"];说实话,我不确定为什么(也许有人可以评论,我可以改进这篇文章!)但是应该这样做特技。

答案 4 :(得分:0)

我在这里遇到了与UIAlertView相同的问题,在我的情况下,我有另一个实现警报的类,而另一个我调用静态方法。如下所示:

ClassA

...

doSomething {
 ... some stuff ...

 [MyAlertView showAlert];

 ... some other stuff...

}

我怀疑的是,当我点击按钮时异步显示警报视图时,对象已经被释放。

为了验证这一点,我更改了代码以实例化警报而不是释放它。每一个人都在工作。

我的最终解决方案是在父视图中声明一个变量,并在取消分配视图时将其与其他变量解除分配。

答案 5 :(得分:0)

这可能是因为从后台线程更新UIKit

我解决了这个问题

UIAlertView *alertMSG = [[UIAlertView alloc] initWithTitle:nil
                        message:@"Your mnessage here"
                        delegate:self
                        cancelButtonTitle:@"Title here"
                        otherButtonTitles: nil];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         [alertMSG show];
    }];
相关问题