UIAlertView无法显示并导致“EXC_BAD_ACCESS”错误 [英] UIAlertView fails to show and results in “EXC_BAD_ACCESS” error

查看:108
本文介绍了UIAlertView无法显示并导致“EXC_BAD_ACCESS”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 is then passed into an UIAlterView and displayed to the user. The alert doesn't have any options (hence why I'm not calling a delegate), but simply notifies the user of what happened.

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

Below is the full method (previously displayed partial). When I comment out everything before the UIAlertView and substitute the string @"test" instead of passing message the Alert is shown successfully. Am I not handling memory correctly with my structure?

- (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 错误。我没有正确发布警报或消息吗?这是控制台输出:

When I execute the code the iPhone greys out like it is about to display an alert but crashes. I get a EXC_BAD_ACCESS error in the console. Am I not releasing either the alert or the message correctly? Here is the console output:

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);

编辑:已删除 [消息发布]; 并根据答案使用 [NSString stringWithString]; 分配字符串。

removed [message release]; and assigned strings using [NSString stringWithString]; based on answers.

推荐答案

我遇到了这样的问题...我从后台线程调用uiAlertView ....从主线程中调用它

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

这篇关于UIAlertView无法显示并导致“EXC_BAD_ACCESS”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆