c++marmalademarmalade-edk

app -> extension crash on callback invocation


I have a Marmalade extension and sample app that passes C-callback to the function from extension, so what happens is whenever extension tries to call the function, it crashes with mysterious message:

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x00000008 Crashed Thread: 0

Although, before I call the function I print it's address and it's different and seems valid. So is it Marmalade restrictions or I am doing something wrong?

p.s: worth to notice I compile my marmalade extension with clang (not gcc) and everything works fine, but callbacks... Although I used default marmalade gcc before and had completely the same issue.

Sample app:

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

The function s3eMyExtDoInitialize exposed from extension looks like that:

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

Solution

  • Well, apparently there's no way to pass a function pointer from app to extension directly, so Marmalade offers their own callbacks system which should be utilized to get things to work.