Jump to content
  • Advertisement
Sign in to follow this  
mckracken

Problem saving/loading bytecodes (angelscript bug?)

This topic is 4028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! I'm trying to save/load to/from bytecode file. But if i use arrays in the script, the application crashes with an access violation when loading the binary file:
// NEXT: Must go through the bytecode and set the correct objecttype pointer where used
int asCRestore::Restore() 
{
	// Before starting the load, make sure that 
	// any existing resources have been freed
	module->Reset();

	unsigned long i, count;

	asCScriptFunction* func;
	asCProperty* prop;
	asCString *cstr;

	// structTypes[]
	READ_NUM(count);
	module->classTypes.Allocate(count, 0);
	for( i = 0; i < count; ++i )
	{
		asCObjectType *ot = NEW(asCObjectType)(engine);
		ReadObjectTypeDeclaration(ot);
		engine->classTypes.PushLast(ot);
		module->classTypes.PushLast(ot);
		ot->refCount++;
	}

	// usedTypes[]
	READ_NUM(count);
	module->usedTypes.Allocate(count, 0);
	for( i = 0; i < count; ++i )
	{
		asCObjectType *ot = ReadObjectType();
		module->usedTypes.PushLast(ot);
		ot->refCount++;		
	}
.
.
.
}
In the last line (ot->refCount++) ot is NULL, so there i have the access violation. Here's the script i've used:
Sprite		spr;
float[]		f(5);

bool Initialize() {
	if (!spr.Load("data/graphics/roxy.spr"))
		return false;

	spr.SetFrame(10);
	spr.SetPos(20,20);

	return true;
}

void Update(float dt) {
	spr.Update(dt);
}

void Draw() {
	spr.Draw();
}
If I comment the float[] f(5); line, the problem dissapear. Is it something i'm doing wrong when registering the arrays? Here is how I registered the floats array:
	if (ScriptMgr::RegisterObjectType("float[]", sizeof(vector<float>), asOBJ_CLASS_CDA) < 0)
		return false;
	if (ScriptMgr::RegisterObjectBehaviour("float[]", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR(ConstructFloatArray, (vector<float> *), void), asCALL_CDECL_OBJLAST) < 0)
		return false;
	if (ScriptMgr::RegisterObjectBehaviour("float[]", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTIONPR(ConstructFloatArray, (int, vector<float> *), void), asCALL_CDECL_OBJLAST) < 0)
		return false;
	if (ScriptMgr::RegisterObjectBehaviour("float[]", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructFloatArray), asCALL_CDECL_OBJLAST) < 0)
		return false;
	if (ScriptMgr::RegisterObjectBehaviour("float[]", asBEHAVE_ASSIGNMENT, "float[] &f(float[]&in)", asMETHODPR(vector<float>, operator=, (const std::vector<float> &), vector<float>&), asCALL_THISCALL) < 0)
		return false;
	if (ScriptMgr::RegisterObjectBehaviour("float[]", asBEHAVE_INDEX, "float &f(int)", asMETHODPR(vector<float>, operator[], (vector<float>::size_type), float &), asCALL_THISCALL) < 0)
		return false;
	if (ScriptMgr::RegisterObjectMethod("float[]", "int length()", asMETHOD(vector<float>, size), asCALL_THISCALL) < 0)
		return false;
Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
I haven't done any extensive testing on the save/load feature, so it's entirely possible that it is a bug in AngelScript itself. I'll have to make some tests with your example to see what I can find.

Regards,
Andreas

Share this post


Link to post
Share on other sites
oh! Okey, it was driving me crazy!!

Well, I'll wait anxious to the next release! ;)

Thanks a lot for the answer and for the fantastic angelscript that i'm using in my games engine!


Share this post


Link to post
Share on other sites
Sorry for taking so long about this, but the bug has finally been fixed. You can get the bug fix in revision 155 in the SVN.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I'm still having problems when saving/loading bytecode. It works well but when i try to resize a script array, it crash with an unhandled exception. Here is the angelscript code i'm using:

const int COLLISION_MAX_ENTITIES = 200;

class CheckCollision {

Actor@[] _list1;
Actor@[] _list2;

int _lastList1;
int _lastList2;

bool Initialize() {
_list1.resize(COLLISION_MAX_ENTITIES);
_list2.resize(COLLISION_MAX_ENTITIES);
_lastList1 = 0;
_lastList2 = 0;
return true;
}
.
.
.
}


The problem resides in the resizes. When I try to resize the arrays, it crashes with an unhandled exception in as_arrayobject.cpp method:


static void ArrayObjectResize(asUINT size, asCArrayObject *self)
{
self->Resize(size);
}


If I try the same code without the preloaded bytecode, it works well.

Thanks!!!!

Share this post


Link to post
Share on other sites
Seems to be some property of the script array types that isn't being properly restored. I'll investigate this. Thanks for reporting it.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I forgot to mention that Actor is a script interface. I don't know if resizing arrays of C++ classes causes the same problem.

Share this post


Link to post
Share on other sites
I'm unable to reproduce this last problem. I wrote the following test case:


static const char *script4 = " \n"
"const int COLLISION_MAX_ENTITIES = 200; \n"
"interface Actor { void Test(); } \n"
"class CheckCollision { \n"
" Actor@[] _list1; \n"
" Actor@[] _list2; \n"
" int _lastList1; \n"
" int _lastList2; \n"
" \n"
" bool Initialize() { \n"
" _list1.resize(COLLISION_MAX_ENTITIES); \n"
" _list2.resize(COLLISION_MAX_ENTITIES); \n"
" _lastList1 = 0; \n"
" _lastList2 = 0; \n"
" return true; \n"
" } \n"
"}";


bool Test()
{
int r;

// Compile and save the byte code
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->AddScriptSection(0, "script", script4, strlen(script4));
r = engine->Build(0);
if( r < 0 ) fail = true;
CBytecodeStream stream4;
engine->SaveByteCode(0, &stream4);
engine->Release();

// Load and execute the byte code
engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->LoadByteCode(0, &stream4);
r = engine->ExecuteString(0, "CheckCollision c; c.Initialize();");
if( r != asEXECUTION_FINISHED )
fail = true;
engine->Release();

return fail;
}






The test passed without any problems.

Would you mind giving the above test a try on your target system to see if it reproduces your problem? I'll need your help to fix the problem. Without being able to reproduce the problem myself it can be next to impossible to fix.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I've ran your test and it works well...

I couldn't figure out what's going on with my code and I don't know how to show you without sending all of it (because it's big to be pasted in the forum).

What else could I try?


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!