🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Obj pointer getting trashed

Started by
0 comments, last by WitchLord 17 years, 4 months ago
Hey Guys, I'm evaluating angelscript for use in a project and have come across a problem. Btw, this could easily be my fault, so some insight would be appreciated. I have a test structure defined in C and exposed to script. Pretty simple. I've provided ref count methods, a Construct function, etc. If I create an instance of TestStruct in c, and pass into angel as an object handle, all is well. The following works beautifully, and all reference callbacks and return values are working.

// Script
int SuperFunc( TestStruct@ weird ) 		
{										
 int i = weird.DoTest1( 12 );		
 int j = i;							
 return j;							
}				
So DoTest1 calls back to C with a proper "this" pointer. However, if I declare an instance of TestStruct within script, and attempt to call any methods on it, I end up with a garbage "this" pointer. The first line properly calls back to my Construct function in C and a newly allocated instance is returned back to angel. On the following line, when angel calls DoTest1, it's pushing a different address onto the stack than was returned from Construct. Somewhere internally, the object pointer got stomped.

// Script
void Crashes()
{
  TestStruct s;
  s.DoTest1( 5 );
}
Any thoughts? Any help is greatly appreciated... thanks in advance. Here's my registration for TestStruct.

// C
pEngine->RegisterObjectType("TestStruct", sizeof(TTestStruct), asOBJ_CLASS_CDA);
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_CONSTRUCT, "void f()",asFUNCTION(TTestStruct::Construct), asCALL_CDECL_OBJLAST);
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_ADDREF,"void f()",asMETHOD(asCScriptString,AddRef), asCALL_THISCALL); 
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_RELEASE,"void f()",asMETHOD(asCScriptString,Release), asCALL_THISCALL);
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_ASSIGNMENT,"TestStruct &f(const string ∈)", asMETHOD(TTestStruct, operator =, 
	(const TString&), TTestStruct&),asCALL_THISCALL);
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_ASSIGNMENT,"TestStruct &f(const TestStruct ∈)", asMETHOD(TTestStruct, operator=, 
	(const TTestStruct&), TTestStruct&),asCALL_THISCALL); 
pEngine->RegisterObjectMethod( "TestStruct", "int DoTest1( int i )", asMETHOD(TTestStruct,DoTest1), asCALL_THISCALL );
[Edited by - delscorcho on February 2, 2007 1:23:55 AM]
Advertisement
Sounds like you haven't implemented the constructor behaviour correctly. It should look something like this (taken from add_on/scriptstring/scriptstring.cpp):

static void ConstructString(asCScriptString *thisPointer){	// Construct the string in the memory received	new(thisPointer) asCScriptString();}


You see, AngelScript will pass you a pointer to the allocated memory. The constructor function should only initialize that memory. It must not allocate new memory, and shouldn't return anything either.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement