Advertisement

ESP Exception Using Callbacks

Started by January 01, 2020 12:22 AM
3 comments, last by WitchLord 4 years, 10 months ago

(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?

I don't see an immediate error in the code you showed. Can you show the complete code so I can try to reproduce the error?

It sounds like the problem is related to the ‘void suspend(C2EventTypes ev)’ function, and how it or the C2EventTypes type is registered with AngelScript.

Are you able to call the suspend function directly from a script without it being from the callback function without it causing the exception?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Just a follow up (and I got it working).

I was allowing the context to go out of scope and lose the function pointer I had previously set.

Glad you got it working ?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement