• 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
Tzarls

Segmentation fault - void asCScriptEngine::CallObjectMethod(...)

16 posts in this topic

Hi, I'm getting a segmentation fault here:

[CODE]
void asCScriptEngine::CallObjectMethod(...)
{
.....f(obj, param); // <- line 3373 - causes segmentation fault
}

[/CODE]

"f" comes out with some weird value.

This function is being called by the asCScriptEngine::WriteMessage(...). I'm registering my callback function like this:
[CODE]
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";

printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}


[/CODE]

I have the feeling that the problem is here (file angelscript.h line 1024)

[CODE]
// Template specialization
template <>
struct asSMethodPtr<SINGLE_PTR_SIZE>
{
template<class M>
static asSFuncPtr Convert(M Mthd)
{
asSFuncPtr p;
asMemClear(&p, sizeof(p));
asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE); //<- after this, "f" (inside "p") gets wrong value
// Mark this as a class method
p.flag = 3;
return p;
}
};
[/CODE]

To make things even weirder, everything works as expected under Windows XP. I'm getting this error under Linux (Ubuntu) - using Code::Blocks as IDE.

Any ideas?

Carlos
0

Share this post


Link to post
Share on other sites
Just tried - under Linux it only works with the non-virtual version.... but under Windows it works with the virtual version too.
0

Share this post


Link to post
Share on other sites
It might be that when taking the address of a virtual method from within the actual class GNUC is returning the method pointer in a different way.

Can you check what the difference is between the following two ways of doing it:

[code]



void global_setMessageCallback(scriptInteface *obj, asIScriptEngine * compiler)
{
compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),obj,asCALL_THISCALL );
}

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);}
[/code]

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.
0

Share this post


Link to post
Share on other sites
Interesting..... when the callback function is not virtual, the
[CODE]

static asSFuncPtr Convert (M Mthd)
[/CODE]

method (angelscript.h line 1029) is getting the Mthd value as :

(void(scriptInterface::*)(scriptInterface *const)) 0x8068370 <scriptInterface::MessageCallback(asSMessageInfo const*)>

But when the callback function is virtual (even if it hasn´t been overridden) is passed as:

&virtual scriptInterface::MessageCallback(asSMessageInfo const*)

(as reported by code::blocks IDE)

Is that ok? Edited by Tzarls
0

Share this post


Link to post
Share on other sites
On gnuc a class method pointer has the following structure

[code]
struct {
union {
void *funcPtr;
int virtualFunctionIndex;
};
int baseOffset;
};
[/code]

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.
0

Share this post


Link to post
Share on other sites
Apparently this isn´t my fault :D

Here´s a minimal code that exhibits the same behaviour:

main.h :

[CODE]
#ifndef _MAIN_
#define _MAIN_
#include <cstdio>
#include <angelscript.h>
class msgClass
{
public:
/*virtual*/ void MessageCallback(const asSMessageInfo *msg) // uncomment "virtual" and the app fails
{
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
};
#endif
[/CODE]

main.cpp :

[CODE]
#include "main.h"
int main (int argc, char ** argv)
{
msgClass *msg = new msgClass;
// Create the script engine
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( engine == 0 )
{
printf("Failed to create script engine.\n");
return -1;
}
engine->SetMessageCallback( asMETHOD(msgClass,MessageCallback),msg,asCALL_THISCALL );
engine->WriteMessage("My Section", 11, 38, asMSGTYPE_INFORMATION, "Testing Message Callback" );
printf ("Done!");
return 0;
}
[/CODE]
1

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
Thanks for the offer. I'll try to reproduce it on my mac first. If I don't succeed I'll definitely need help in figuring out how to make this work.
0

Share this post


Link to post
Share on other sites
One of the internal functions for calling class methods didn't have the implementation to support virtual class methods. Somehow this had gone unnoticed for many years.

[url="http://angelscript.svn.sourceforge.net/viewvc/angelscript/trunk/sdk/angelscript/source/as_scriptengine.cpp?r1=1333&r2=1338"]Here's the diff showing what was changed.[/url]

Regards,
Andreas
0

Share this post


Link to post
Share on other sites
FWIW, I´ve just tested this fix in my original project and it works as expected. Thanks again!
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