• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Marcus L

Android - virtual function call issue

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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? 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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. :)

0

Share this post


Link to post
Share on other sites

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

Thanks for your help Andreas!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0