Advertisement

Crash instancing a new class inside as

Started by April 02, 2007 07:05 AM
2 comments, last by WitchLord 17 years, 7 months ago
Hello. I'm trying to integrate as into my app to add scripting support. User must implement a function with this signature: void initScriptDesc(ScriptDesc &out desc); And fill ScriptDesc class with some datas. ScriptDesc contains some general infos about script (title, author, email...) and a number of actions that user could define thru the ActionDesc class. Here a sample: void initScriptDesc(ScriptDesc &out desc); { desc.title = "My script"; desc.author = "..."; desc.website = "..."; desc.email = "..."; ActionDesc a; a.title = "My action #1"; a.onLoad = "..."; desc.addAction(a); a.title = "My action #2"; desc.addAction(b); } "ActionDesc a;" crashes my application when i use this code: ScriptDesc desc; int getInfo; getInfo = m_engine->GetFunctionIDByDecl(0, "void initScriptDesc(ScriptDesc &out desc)"); // Execute the script function m_context->Prepare(getInfo); m_context->SetArgObject(0, &desc); int r = m_context->Execute(); I can't guess which is the reason... I register ActionDesc in this way: m_engine->RegisterObjectType("ActionDesc",sizeof(ActionDesc), asOBJ_CLASS); m_engine->RegisterObjectType("ScriptDesc",sizeof(ScriptDesc), asOBJ_CLASS); m_engine->RegisterObjectProperty("ActionDesc", "string onLoad", offsetof(ActionDesc,onLoad)); m_engine->RegisterObjectProperty("ActionDesc", "string onUnload", offsetof(ActionDesc,onUnload)); m_engine->RegisterObjectProperty("ActionDesc", "string onInit", offsetof(ActionDesc,onInit)); m_engine->RegisterObjectProperty("ActionDesc", "string onUninit", offsetof(ActionDesc,onUninit)); m_engine->RegisterObjectProperty("ActionDesc", "string onStep", offsetof(ActionDesc,onStep)); m_engine->RegisterObjectProperty("ActionDesc", "string onContact", offsetof(ActionDesc,onContact)); m_engine->RegisterObjectProperty("ActionDesc", "string onScore", offsetof(ActionDesc,onScore)); m_engine->RegisterObjectProperty("ActionDesc", "string title", offsetof(ActionDesc,title)); m_engine->RegisterObjectProperty("ActionDesc", "string description", offsetof(ActionDesc,description)); Thanks in advance for replies!
ActionDesc holds a lot of strings as members. Did you register the constructor for this type? In addition to that you'll need to register either the addref/release behaviours or the destructor.

Without the constructor AngelScript will simply allocate the memory, but will not initialize it because it wouldn't know how.

The constructor is a rather simple wrapper:

void ActionDesc_Construct(void *ptr){  new(ptr) ActionDesc; // Initialize the preallocated memory}engine->RegisterObjectBehaviour("ActionDesc", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ActionDesc_Construct), asCALL_CDECL_OBJLAST);


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

Advertisement
Ok thanks now it works. I didn't realize it. I thought that it was mandatory only for custom ctor...

So you don't call c-tor, why don't you call it automagically?
I've insert dtor beaviour too, is it necessary for right std::string cleanup?

Script support in my app it's really wonderful :)
If I could I would have done it automatically. Unfortunately it is not possible to determine the address of the constructor at runtime. You can't even take the pointer to the constructor in the C++ language, which is why it is necessary to write a light wrapper for it.

The destructor is necessary if you don't use reference counting. Without the destructor or the addref/release behaviours AngelScript will only free the main memory block allocated for the class, not the dynamic buffers allocated by the string members.

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