Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


- - - - -

Android - virtual function call issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Bitsauce   Members   -  Reputation: 235

Like
0Likes
Like

Posted 05 January 2014 - 07:06 PM

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


Edited by Macobyte, 05 January 2014 - 07:08 PM.


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
0Likes
Like

Posted 05 January 2014 - 08:05 PM

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

#3 Bitsauce   Members   -  Reputation: 235

Like
0Likes
Like

Posted 06 January 2014 - 08:36 AM

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



#4 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
0Likes
Like

Posted 06 January 2014 - 12:19 PM

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

#5 Bitsauce   Members   -  Reputation: 235

Like
0Likes
Like

Posted 06 January 2014 - 02:35 PM

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!


Edited by Macobyte, 06 January 2014 - 03:03 PM.


#6 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
0Likes
Like

Posted 06 January 2014 - 02:57 PM

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. 


Edited by Andreas Jonsson, 06 January 2014 - 02:58 PM.

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

#7 Bitsauce   Members   -  Reputation: 235

Like
0Likes
Like

Posted 06 January 2014 - 03:04 PM

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


Edited by Macobyte, 06 January 2014 - 03:06 PM.


#8 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
1Likes
Like

Posted 06 January 2014 - 03:35 PM

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

#9 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
0Likes
Like

Posted 06 January 2014 - 04:46 PM

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

#10 Bitsauce   Members   -  Reputation: 235

Like
0Likes
Like

Posted 06 January 2014 - 05:26 PM

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

Thanks for your help Andreas!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS