实现系统级的模态对话框(MessageBox),iOS版偶尔按HOME再切回去时莫名崩溃.

症状:
当消息框弹出后,按Home键切出来,再重新进程序,然后点消息框上的取消按钮,当消息框关闭后,程序偶尔(只是偶尔)会报错,
有时是EXC_BAD_INSTRUCTION 有时是EXC_BAD_ACCESS 感觉像是堆栈坏了 导致返回的时候乱执行 ? 而很多时候又不会报错,很正常

我是在一个schedule定时器回调中调用MessageBox_Mz(只会调用一次不会反复调用)的,iOS部分实现代码如下:

// 模态消息框代理
@interface ModalDelegate_Mz : NSObject
{
bool mRunLoop;
}

  • (void)showModalAlert:(UIAlertView *)alertView;
    @end // ModalDelegate_Mz

@implementation ModalDelegate_Mz

  • (void)showModalAlert:(UIAlertView *)alertView
    {
    retain];
    setDelegate:self];
    show];

    mRunLoop = true;
    while (mRunLoop)
    {
    NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:NSDate distantFuture]];
    // runMode:NSDefaultRunLoopMode beforeDate:];
    }

    setDelegate:nil];
    release];
    }

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    mRunLoop = false;
    }
    @end // ModalDelegate_Mz

void MessageBox_Mz(const char * msg, const char * title)
{
ModalDelegate_Mz * modalDelegate = ModalDelegate_Mz alloc] init];

NSString * tmpTitle = (title) ? NSString stringWithUTF8String : title] : nil;
NSString * tmpMsg = (msg) ? NSString stringWithUTF8String : msg] : nil;
UIAlertView * messageBox = UIAlertView alloc] initWithTitle: tmpTitle
                                                      message: tmpMsg
                                                     delegate: nil
                                            cancelButtonTitle: @"OK"
                                            otherButtonTitles: nil];

//;
showModalAlert:messageBox];

//开始怀疑是这两句引起的,注释掉一样会报错
//;
//;

}

错误截图在附件中

设备(ipad4) iOS版本7.1
xCode 5.1
Cocos2d-x 3.0

搞定了 换成点击button时回调 而不是强制内部进行消息循环 即可
.
为Cocos2d-x增加带Button回调的系统级消息框(MessageBox)
http://blog.csdn.net/liumazi/article/details/42082793

楼主牛!:891:学习了。