我有一个 Marmalade 扩展程序和示例应用程序,它从扩展程序将 C 回调传递给函数,所以每当扩展程序尝试调用该函数时,它就会崩溃并显示神秘消息:
异常类型:EXC_BAD_ACCESS (SIGSEGV) 异常代码:0x00000008 处的 KERN_INVALID_ADDRESS 线程崩溃:0
虽然,在我调用该函数之前,我打印了它的地址,它是不同的并且看起来是有效的。那么是果酱限制还是我做错了什么?
ps:值得注意的是,我用clang(不是gcc)编译了我的果酱扩展,一切正常,但是回调......虽然我之前使用了默认的果酱gcc并且遇到了完全相同的问题。
示例应用程序:
class CDemoApp {
public:
static CDemoApp* getInstance() {
static CDemoApp instance;
return &instance;
}
void setup()
{
app = CreateApp();
window = CreateWindow();
app->AddWindow(window);
view = CreateView("canvas");
button = CreateButton();
button->SetEventHandler("click", (void*)NULL, CDemoApp::onButton1Click);
view->AddChild(button);
window->SetChild(view);
app->ShowWindow(window);
}
void run() {
app->Run();
}
static void onSDKInit(const char* err, void* context) {
printf("Done.\n");
}
static bool onButton1Click(void* data, CButton* button) {
if(!s3eMyExtDidInitialize()) {
// crash happens here.
// extension doesn't perform any ops, just calls the callback with dummy arguments
s3eMyExtDoInitialize(APP_ID, APP_SECRET, CDemoApp::onSDKInit, NULL);
}
return true;
}
private:
CDemoApp() {};
private:
CAppPtr app;
CWindowPtr window;
CViewPtr view;
CButtonPtr button;
};
// Main entry point for the application
int main()
{
if(!s3eMyExtAvailable())
{
s3eDebugErrorShow(S3E_MESSAGE_CONTINUE, "My extension is not found");
return 0;
}
CDemoApp::getInstance()->setup();
CDemoApp::getInstance()->run();
return 0;
}
从扩展中暴露的函数s3eMyExtDoInitialize
如下所示:
typedef void (*my_callback)(const char* error, void* context);
void s3eMyExtDoInitialize_platform(const char* appId, const char* appSecret, my_callback callback, void* context) {
callback(NULL, NULL);
}