gpt4 book ai didi

objective-c - 使用 NSRunAlertPanel 时的 EXC_BAD_INSTRUCTION

转载 作者:行者123 更新时间:2023-12-03 16:46:37 26 4
gpt4 key购买 nike

为了在某些情况下显示错误,我在 Mac OS X 上使用 NSRunAlertPanel(在从 Windows 移植的代码上,我在 Windows 上使用 MessageBox)。

在实际创建 Windows 之前,正在运行一些代码并调用此代码来显示一些条件错误。

在线程 com.apple.libdispatch-manager 中,在以下调用堆栈下

0 _dispatch_mgr_invoke
1 _dispatch_mgr_thread

它给出了 EXC_BAD_INSTRUCTION

是不是因为Windows没有先于NSRunAlertPanel创建?

这个运行时错误的原因是什么? Mac OS X 上 MessageBox 的确切替代方案是什么?

Long ShowDebugMessageBox (const wchar_t * Message, const wchar_t * Title)
{
NSString * message; ///< Message.
NSString * title; ///< Title.
NSInteger response; ///< response.

message = WideToNSString (Message);
title = WideToNSString (Title);


//response = NSRunAlertPanel(title, message, @"Yes", @"No", @"Cancel");
response = NSRunCriticalAlertPanel (title, message, @"Okay", @"Cancel", nil);

switch(response) {
case NSAlertDefaultReturn:
return IDYES;
case NSAlertAlternateReturn:
return IDNO;

default:
return IDCANCEL;
}

}


NSString * WideToNSString (const wchar_t * Str)
{
if(!Str) {
return nil;
}

NSString * str; ///< String in NSString.
#if CP_SIZEOFWCHAR == 4
str = [[NSString alloc] initWithBytes: (CVPtr) Str
length: sizeof(wchar_t)* wcslen(Str)
encoding: NSUTF32LittleEndianStringEncoding];
//encoding: NSUTF32StringEncoding];
#else

str = [[NSString alloc] initWithBytes: (CVPtr) Str
length: sizeof(wchar_t)* wcslen(Str);
encoding: NSUTF16LittleEndianStringEncoding];
//encoding: NSUTF16StringEncoding];
#endif

return str;
}

class File {
public:
int Open(char * fname, int mode)
{
fd = open(fname, mode);

}

int Close()
{
close(fd);
//fd = 0; //CAUSE of the PROBLEM
}

~File ()
{
//ALERT Display message box about the error.
ALERT(fd != 0);
}

private:
int fd;

};

这是显示消息框的代码。

从 wchar_t * string(宽字符串)获取 NSString 的代码非常好并且已经过测试。它被用在很多地方并且运行良好。

另一个应用程序(首先创建窗口)上的相同代码运行良好。

调用 File 的析构函数时出现问题。由于 fd 不为 0,因此会显示消息框并导致问题。

当fd设置为0时,构造函数不会显示警告框。然而,显示了其他警报,但没有出现问题。

到期了吗?

最佳答案

您没有提供足够的信息来说明导致异常的原因(请显示代码)。

我使用NSRunCriticalAlertPanel()在我的应用程序中显示 fatal error ,我几乎可以随时调用它:

void criticalAlertPanel(NSString *title, NSString *fmt, ...)
{
va_list va;
va_start(va, fmt);
NSString *message = [[NSString alloc] initWithFormat:fmt arguments:va];
va_end(va);
NSRunCriticalAlertPanel(title, message, @"OK", nil, nil);
}

(此代码启用了 ARC)。

关于objective-c - 使用 NSRunAlertPanel 时的 EXC_BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com