Jump to content



- - - - -

Crash when copying obj_values

  • You cannot reply to this topic
1 reply to this topic

#1 Plaristote   Members   -  Reputation: 102

Like
0Likes
Like

Posted Today, 01:09 PM

Greetings !
I would need a bit of help on fixing a problem : apparently I'm doing something wrong when implementing the opAssign operator.
Here is a short and easily reproducible piece of code that, for some reason, crashes :
struct test
{
test copy(test other) { std::cout << "test2" << std::endl; thing = other.thing; return (other); }

int thing;
};

int main(void)
{
asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

const char* testClass = "Test";
engine->RegisterObjectType (testClass, sizeof(test), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS);
engine->RegisterObjectMethod (testClass, "Test opAssign(Test)", asMETHOD(test,copy), asCALL_THISCALL);

CScriptBuilder builder;

builder.StartNewModule(engine, "testModule");
builder.AddSectionFromFile("scripts/t.as");
builder.BuildModule();

asIScriptModule* module = engine->GetModule("testModule");
asIScriptFunction* func = module->GetFunctionByDecl("void testFunc()");
asIScriptContext* context = engine->CreateContext();

std::cout << "Prepare" << std::endl;
context->Prepare(func);
std::cout << "Execute" << std::endl;
context->Execute();
std::cout << "Done" << std::endl;

return (0);
}
And the AngelScript code :
void testFunc()
{
Test test;
Test test2;

test = test2;
}
It crahses at the line "test = test".
Can you please help me :) ?

Ad:

#2 Andreas Jonsson   Moderators   -  Reputation: 1261

Like
0Likes
Like

Posted Today, 03:35 PM

You have a bit of a chicken-and-egg situation here.

Your opAssign method is taking the object by value, but in order to pass the object by value to the method the object needs to be copied, which in turn will call the opAssign method. :)

I'll have to add a check in the code to prevent the script engine to accept this as it obviously won't work.

To fix it in your code you need to change your opAssign() method to take the object by reference, the opAssign() method should also return a reference to the object, i.e:


struct test
{
test &copy(const test &other) { std::cout << "test2" << std::endl; thing = other.thing; return *this; }

int thing;
};

int main(void)
{
asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

const char* testClass = "Test";
engine->RegisterObjectType (testClass, sizeof(test), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS);
engine->RegisterObjectMethod (testClass, "Test &opAssign(const Test &in)", asMETHOD(test,copy), asCALL_THISCALL);

Observe how the C++ signature is 'test &copy(const test &)' but the AngelScript signature is 'Test &opAssign(const Test &in)'. The 'in' keyword is important, as it tells AngelScript that this reference is meant to be an input value.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game






We are working on generating results for this topic
PARTNERS