This function is being called by the asCScriptEngine::WriteMessage(...). I'm registering my callback function like this:
void scriptInterface::setMessageCallback(asIScriptEngine* compiler)
{
compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),this,asCALL_THISCALL );
}
virtual void scriptInterface::MessageCallback(const asSMessageInfo *msg)
{
// Implement a simple message callback function
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
void scriptInterface::setMessageCallback(asIScriptEngine* compiler){ // Get the method from within the class compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),this,asCALL_THISCALL ); // Get the method from a global function global_setMessageCallback(this, compiler);}
If there is a difference between these two ways of doing it, there has to be a way of identifying that difference so the call can be made correctly anyway.
On gnuc a class method pointer has the following structure
struct {
union {
void *funcPtr;
int virtualFunctionIndex;
};
int baseOffset;
};
If the method is a virtual method, then union will have its least significant bit set, i.e. (int(funcPtr) & 1) will be true, otherwise it will be false, meaning that the funcPtr is the absolute address of the function that will be called.
If the class for the method pointer doesn't have multiple base classes, the baseOffset will always be 0, otherwise it will be the offset that must be added to the 'this' pointer to point to the correct base class when calling the method.
When calling a virtual method, the correct address of the function must be looked up in the virtual function table. The pointer to the virtual function table is stored as the first pointer in the class, i.e. you get to it by derefencing the this pointer, then you get the index into this table by dividing the virtualFunctionIndex by 4.
Of course, if the this pointer is corrupt, or if the first pointer in the class has been overwritten, for example by a buffer overflow or other memory invasion, then the function pointer retrieved from the virtual function table will be incorrect and most likely crash the application.
With what you describe it seems your problem is the latter, i.e. the this pointer is either wrong, or the pointer to the virtual function table has been overwritten. As your message callback doesn't really use the object, the crash doesn't happen when the method is not virtual.
I suggest you check the address at the first position in the scriptInterface. If the address is different when you register the message callback, and when the segv happens, then you definitely have a memory issue in your code. If that is so, you should be able to set a memory breakpoint on that address to identify where that memory issue is occurring.
It's good that you managed to narrow it down to this. Of course, it means there is a problem in AngelScript but I'm sure there is a solution. Hopefully it won't be too difficult to find.