Android - virtual function call issue

Started by
8 comments, last by Marcus L 10 years, 3 months ago

Hi again!
I've encountered a weird problem when calling virtual functions on Android (ARM version). It seems like I'm getting a crash whenever I call a inherited function, whether or not it is overwritten by a derived class. For example if I have these two classes:


class Base
{
public:
    virtual void printA()
    {
        LOGI("Base: PrintA called");
    }
    
    virtual void printB()
    {
        LOGI("Base: PrintB called");
    }
};

class Derived : public Base
{
public:
    void printA()
    {
        LOGI("Derived: PrintA called");
    }
};

And I register printA and printB at some point using the following:


scriptEngine->RegisterGlobalFunction("void printA()", asMETHOD(Base, printA), asCALL_THISCALL_ASGLOBAL, derivedObj);
scriptEngine->RegisterGlobalFunction("void printB()", asMETHOD(Base, printB), asCALL_THISCALL_ASGLOBAL, derivedObj);

If I now call either printA or printB in AngleScript, my Android application will crash.

Now, as a disclaimer, I might have messed up my configuration at some point, however I don't know how to track down the problem if it in fact is a configuration problem. I've made sure __GNUC__ and ANDROID is defined. I figure it might have something with the VIRTUAL_BASE_OFFSET macro, but, then again, my lack of the technical know-how makes it difficult for me to track the exact problem down.

Has anyone else got something like this to work?

As a side note; I have managed to register and call simple global functions and different value type objects (strings and math objects), so the problem seems to be quite specific. Also, I cannot explain this, but in my main application, I'm actually able to call some function which are registered in this way (but mostly not), but I'm not able to find any obvious pattern among them unsure.png

Anyways, looking forward to some feedback! smile.png

Advertisement

You're registering the address of the base class' method but you're supplying a pointer to the derived class. This is most likely what is causing the problem for you.

In the simple sample you provided, it shouldn't matter because the derived class has only one base class, but if your derived class inherits from multiple classes it makes a big difference. When there are multiple base classes the virtual base offset will be different for each of the base methods so that the object pointer can be correctly adjusted to point to the part of the object that represents the respective base class.

Regards,

Andreas

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

Okay, so I changed my register calls to the following:


scriptEngine->RegisterGlobalFunction("void printA()", asMETHOD(Derived, printA), asCALL_THISCALL_ASGLOBAL, derivedObj);
scriptEngine->RegisterGlobalFunction("void printB()", asMETHOD(Derived, printB), asCALL_THISCALL_ASGLOBAL, derivedObj);

However, I still get the crash.

Also, on windows, registering my functions using a pointer to the Base class method, and the base class object, actually works just fine. When I call the script functions in AngelScript on windows the Derived's implementation is called, if any. The reason I want to do it this way is because my main application acts like a framework, or a AS wrapper, where platform-specific functionality is implemented thought abstract classes (well, kinda-abstract classes). Is it just coincidental that it works for my windows build, maybe?

I wish I could provide some debug information, but retrieving debug info from Android seems near impossible wacko.png . Especially since the crash resides in a Android library (libc.so).

Anyways, thanks for responding! smile.png

You can register the base class' method if you also make sure the object pointer is to the base class. E.g.

Base *baseObj = static_cast<Base*>(derivedObj);
scriptEngine->RegisterGlobalFunction("void printA()", asMETHOD(Base, printA), asCALL_THISCALL_ASGLOBAL, baseObj);
scriptEngine->RegisterGlobalFunction("void printB()", asMETHOD(Base, printB), asCALL_THISCALL_ASGLOBAL, baseObj);

Each compiler has their own way of dealing with method pointers and resolving virtual methods. For example, the method pointers on MSVC have different sizes varying from 4 to 32 bytes depending on the complexity of the class. On gcc the method pointer is always 8 bytes.

Normally you don't need to worry about this though. You just need to make sure that the you use the method pointer and object pointer from the same class declaration in the hierarchy, and the compiler will take care of the rest.

Getting back to the crash on Android. Since it still crashes with the correct registration it looks like it could be a bug in the AngelScript library.

I'll need more information from you to narrow it down to the exact problem. Does the problem only happen with class methods registered as global functions, i.e. asCALL_THISCALL_ASGLOBAL? Or does it also happen for class methods registered as class methods?

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

As far as my investigation goes, it appears it only happens to functions which are registered using asCALL_THISCALL_ASGLOBAL. I tested asCALL_THISCALL with a simple reference type class, and it worked just fine. I even tested what would happen if I registered a global property of type Derived, then called a virtual function of Base registered with asCALL_THISCALL, and it still worked. So the issue seems to be specifically regarding asCALL_THISCALL_ASGLOBAL, when calling a method at an object which inherits another (EDIT: also tested now; the derived object doesn't even have to implement any virtual function for it to crash). Here is an updated and corrected sample which will produce the crash:


class Base
{
public:
	virtual void printA()
	{
		LOGI("Base: PrintA called");
	}
	
	virtual void printB()
	{
		LOGI("Base: PrintB called");
	}
};

class Derived : public Base
{
public:
	void printA()
	{
		LOGI("Derived: PrintA called");
	}
};

// ... registering part
Derived *derived = new Derived();
Base *base = static_cast<Base*>(derived);
scriptEngine->RegisterGlobalFunction("void printA()", asMETHOD(Base, printA), asCALL_THISCALL_ASGLOBAL, base);
scriptEngine->RegisterGlobalFunction("void printB()", asMETHOD(Base, printB), asCALL_THISCALL_ASGLOBAL, base);

Both printA and printB will crash.
Thanks again for your help!

The new code you're showing has an error in the object pointer you're registering with AngelScript. You're passing in a pointer to the pointer to the object, i.e. **base. You should pass the pointer to the object directly.

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

Oh sorry, Fixed it, I didn't have this in my original code though. (the wrath of the copy-paste tongue.png ...)

I've managed to reproduce the problem on my BeagleBone with Linux, which shares the code for the native calling convention with Android.

I'll investigate this problem further and update you as soon as I have a fix for it.

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

I've fixed this in revision 1811.

Interestingly enough the bug was in a part of the code that is not platform specific, and would even have caused problem on Windows if the class used multiple inheritance. I guess THISCALL_ASGLOBAL is not used a lot with virtual methods. :)

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

Hah, nice biggrin.png . Tested the revision, and it worked in the emulator!

Thanks for your help Andreas!

This topic is closed to new replies.

Advertisement