(using a modified Events example)
I'm getting an exception in ctx->Execute() when returning from a callback. It takes a bit of explaining below, but hopefully you'll follow.
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention. occurred
Following the Developers Guide, in the App, I registered a funcdef and function for setting a callback:
// Register a simple funcdef for the callback
engine->RegisterFuncdef("void CallbackFunc()");
// Register a function for setting the callback
engine->RegisterGlobalFunction("void setSafeHandler(CallbackFunc @cb)", asFUNCTION(setSafeHandler), asCALL_CDECL);
Then define the set callback function as:
void setSafeHandler(asIScriptFunction* cb)
{
// Release the previous callback, if any
if (cpcAttributes.safeHandler)
cpcAttributes.safeHandler->Release();
// Store the received handle for later use
cpcAttributes.safeHandler = cb;
}
In the .as script, I set the callback function with:
// change safe handler function
setSafeFunction(SafeHandler1);
where SafeHandler1 is define in the script as:
void SafeHandler1()
{
C2EventTypes ev = C2EventTypes::EvSafe;
suspend(ev); // calls App C++ suspend() to suspend the main context (not the event context)
}
suspend is defined in the App as:
void suspend(C2EventTypes ev)
{
if (cpcAttributes.mainCtx != NULL)
cpcAttributes.mainCtx->Suspend();
}
Getting back to the Events example app, a keypress 's' creates the exception when returning from the script callback
case 's':
{
if ((C2EventTypes::EvResume == (cpcAttributes.m_allowableEvents | C2EventTypes::EvResume)))
{
// call registered Safe Handler
if (cpcAttributes.safeHandler != NULL)
{
eventCtx->Prepare(cpcAttributes.safeHandler);
eventCtx->Execute(); // EXCEPTION AFTER THE SCRIPT CALLBACK FUNCTION COMPLETES;
}
}
break;
}
Am I using the eventCtx->Prepare() method correctly and have I declared the callback correctly?