Sign in to follow this  
Braden M

Problem with POD class ref array on AngelScript 2.25.2

Recommended Posts

Braden M    126
Never mind ... I solved this right after posting [img][/img]

I'm not sure if I'm [ab]using AngelScript incorrectly, or if the garbage collector has a bug, but with the following testcase, I am unable to destroy a dynamic configuration group under certain conditions, involving mentions in the code of an array of refs to C++ POD struct objects.

The code is below, and the exception asCONFIG_GROUP_IS_IN_USE/"Unable to remove the dynamic configuration group." is getting thrown at the very end. Note that I'm not actually running any code; just building it. It seems that the engine is not freeing the "NotFreed" class type correctly. But maybe I'm doing something wrong? Any help would be appreciated.

#include <angelscript.h>
#include <angelscript/add_on/scriptstdstring.h>
#include <angelscript/add_on/scriptarray.h>
int main() {

asIScriptEngine* p_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
int r = p_engine->SetMessageCallback(asFUNCTION(&YourPreferredASCallback), 0, asCALL_CDECL); assert( r >= 0 );

RegisterStdString(p_engine); // Use STL strings as our string type
RegisterScriptArray(p_engine, false); // Enable support for arrays
// The problematic struct
struct DoublePair { double a, b; };

r = p_engine->BeginConfigGroup("exampleGroup"); assert( r >= 0);
r = p_engine->RegisterObjectType("DoublePair", sizeof(DoublePair), asOBJ_VALUE|asOBJ_APP_CLASS|asOBJ_POD); assert(r >= 0); // |asOBJ_APP_CLASS_ALLFLOATS ?
asIScriptModule* p_module = p_engine->GetModule("gcErrorExample", asGM_ALWAYS_CREATE);
p_module->AddScriptSection("example", SCRIPT_SOURCE_INLINE(
class NotFreed {
string name;
NotFreed( DoublePair test ) { }
class OtherClass {
OtherClass() {}
array<NotFreed@> my_array;
void SomeMethod() {
for (uint i=0;i<my_array.length();i++) {
// If the following line is commented out, everything works fine.
my_array[i].name = "test";
// Another way to trigger the error: (uncomment the next line)
//void OtherMethod(array<NotFreed@> arg) {}
r = p_module->Build(); assert(r >= 0);
r = p_engine->GarbageCollect(); assert( r >= 0);
r = p_engine->DiscardModule("gcErrorExample"); assert( r >= 0);
r = p_engine->RemoveConfigGroup("exampleGroup");
if (r == asCONFIG_GROUP_IS_IN_USE) {
throw std::runtime_error("Unable to remove the dynamic configuration group."); // This gets thrown
} else { assert( r >= 0 ); }
[/CODE] Edited by Braden M

Share this post

Link to post
Share on other sites
Braden M    126
Wow, never mind. Solved my own problem just after I posted! I had to swap GarbageCollect() and DiscardModule() at the end, and now it all works. I can't see how to delete this thread though... Edited by Braden M

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this