Jump to content
  • Advertisement
Sign in to follow this  
delscorcho

Obj pointer getting trashed

This topic is 4239 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

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 &in)", asMETHOD(TTestStruct, operator =, 
	(const TString&), TTestStruct&),asCALL_THISCALL);
pEngine->RegisterObjectBehaviour("TestStruct", asBEHAVE_ASSIGNMENT,"TestStruct &f(const TestStruct &in)", 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]

Share this post


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

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!