Sign in to follow this  
Maega

Few more problems

Recommended Posts

Hello. It's me again :). Now that I've cleared up the earlier problems thanks to WitchLord's wonderful help, I have a few more problems. Two of them may be linked, but I'm not sure. One of the class methods I registered causes things to crash right away. I'm not sure why. It's a function that works when used by the class itself external to scripting. It is passed a std::string. Another function will execute, but the string that is supposed to be passed to it from the script doesn't come through. It gets displayed as a square block. After this function is called, I get an immediate crash. If I take this call out and the script is basically empty, no crash occurs. It just calls MessageBoxA This may be something I'm doing. I'm not sure.

Share this post


Link to post
Share on other sites
Usually these kind of crashes happen when you're not registering the types or functions correctly. When you're registering the functions you're telling AngelScript how the application works with these functions. If you get something wrong in the registration then AngelScript will call the function incorrectly, which can lead to memory violations and stack corruptions, eventually crashing the application.

Can you show us how you're registering this class method and it's implementation? Also how are you registering the string type? Are you using the CScriptString add-on or are you registering your own string type?

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
Usually these kind of crashes happen when you're not registering the types or functions correctly. When you're registering the functions you're telling AngelScript how the application works with these functions. If you get something wrong in the registration then AngelScript will call the function incorrectly, which can lead to memory violations and stack corruptions, eventually crashing the application.

Can you show us how you're registering this class method and it's implementation? Also how are you registering the string type? Are you using the CScriptString add-on or are you registering your own string type?


I am using the addon.

How I am registering them is the same way you saw in my registration problem post.

I might have left one out, but the one crashing right off the bat is "FindText"


r = engine->RegisterObjectMethod("CEngine", "void FindText(string &in)", asMETHOD(CEngine, FindText), asCALL_THISCALL); assert( r >= 0 );




Both are the same signature.

ShowMessage(std::string)
FindText(std::string)

Share this post


Link to post
Share on other sites
The problem is that you're registering the function as taking a string by reference, but in truth it takes a string by value.

Unfortunately since you use the add-on you won't be able to register this function as-is, since the add-on is a reference type which cannot be passed by value to application functions. You have three options:

1. Change your function to take a std::string by reference.
2. Write a wrapper function that takes a std::string by reference and calls the real function.
3. Stop using the add-on and instead register a string type as a value type.

Regards,
Andreas

Share this post


Link to post
Share on other sites
Is there a chart that states what corresponds to what C++ types are in Angelscript? I'm trying to register a function with ints this time and it's crashing again. I see the chart in the manual, but I tried what was there and I still crash.

Share this post


Link to post
Share on other sites
Here are all the types in AngelScript and their corresponding types in C++: Manual: Datatypes in AngelScript and C++

Primitive types usually don't cause this kind of crashes. You might get an ESP error if you have the wrong number of arguments, or if the return type is registered incorrectly.

Show me the function you're trying to register, and how you're registering it and I'll tell you what's wrong.

Share this post


Link to post
Share on other sites

r = engine->RegisterObjectMethod("CEngine", "void BuildButton(string &in, int, int, int, int, string &in)", asMETHOD(CEngine, BuildButton), asCALL_THISCALL); assert( r >= 0 );




void CEngine::BuildButton(string& caption, int x, int y, int width, int height, string& function)
{

CButton *button = new CButton();

button->id = rand();
button->function = function;
button->window = CreateWindowExA(0, "BUTTON", caption.c_str(), BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, x, y, width, height, window, (HMENU)button->id, NULL, NULL);
bList.push_back(button);

}



I also put a MessageBox call in there to easily trace whether or not it even got called. The box never showed, so I assume it doesn't actually get into the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
I don't see anything wrong with this. I need more information to discover the cause for the crash.

How is this function called from the script?



blah.BuildButton("Test", 0, 0, 100, 100, "test");




Quote:
And how is the CEngine type registered with the engine?



HWND g_Window;

static void Constructor(void *memory)
{

new(memory) CEngine(g_Window);

}
static void Destructor(CEngine *p)
{

p->~CEngine();
}


CLoad::CLoad(HWND win)
{


ostringstream errorMessage;

engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

int r = engine->SetMessageCallback(asMETHOD(CLoad, GetScriptError), this, asCALL_THISCALL); assert( r >= 0 );

RegisterScriptString(engine);

r = engine->RegisterObjectType("CEngine", sizeof(CEngine), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert(r >= 0);

r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert(r >= 0);

r = engine->RegisterObjectMethod("CEngine", "void ShowMessage(string &in)", asMETHOD(CEngine, ShowMessage), asCALL_THISCALL); assert( r >= 0 );

r = engine->RegisterObjectMethod("CEngine", "void FindText(string &in)", asMETHOD(CEngine, FindText), asCALL_THISCALL); assert( r >= 0 );

r = engine->RegisterObjectMethod("CEngine", "void BuildButton(string &in, int, int, int, int, string &in)", asMETHOD(CEngine, BuildButton), asCALL_THISCALL); assert( r >= 0 );

r = engine->RegisterObjectMethod("CEngine", "void LoadExtension(string &in)", asMETHOD(CEngine, Extension), asCALL_THISCALL); assert( r >= 0 );

g_Window = win;


Share this post


Link to post
Share on other sites
I still don't see anything wrong.

I wrote a small test based on your information and it works without crashing.


class CEngine
{
public:
CEngine() {}
~CEngine() {}

void BuildButton(std::string& caption, int x, int y, int width, int height, std::string& function)
{
assert( caption == "Test" );
assert( x == 0 );
assert( y == 0 );
assert( width == 100 );
assert( height == 100 );
assert( function == "test" );
}
};

static void Constructor(void *memory)
{

new(memory) CEngine();

}
static void Destructor(CEngine *p)
{

p->~CEngine();
}


bool Test2()
{
int r;
bool fail = false;

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

RegisterScriptString(engine);

r = engine->RegisterObjectType("CEngine", sizeof(CEngine), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert(r >= 0);

r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert(r >= 0);

r = engine->RegisterObjectMethod("CEngine", "void BuildButton(string &in, int, int, int, int, string &in)", asMETHOD(CEngine, BuildButton), asCALL_THISCALL); assert( r >= 0 );


r = engine->ExecuteString(0, "CEngine blah; blah.BuildButton('Test', 0, 0, 100, 100, 'test');");
if( r != asEXECUTION_FINISHED )
{
fail = true;
}


engine->Release();

return fail;
}



Are you sure the problem is with this method, and not something else?

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
I still don't see anything wrong.

I wrote a small test based on your information and it works without crashing.

*** Source Snippet Removed ***

Are you sure the problem is with this method, and not something else?


Well, I'm not sure. If I remove the call to that function in the script, it doesn't fail.

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