# Assertion in garbage collector fails with array of asOBJ_REF

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

## Recommended Posts

I am making an RTS game where the script can get an array of units from the application, and select one of them as a target to shoot at.

The function to do this is registered like:

se->RegisterObjectMethod("Entity", "Entity@[]@ getAttackableEnemies()", asMETHOD(Entity,getAttackableEnemies), asCALL_THISCALL);


... and this is implemented by filling a CScriptArray of the type given by:

se->GetTypeIdByDecl("array<Entity@>")

... and the script uses it like this:

Entity@[] enemies = getAttackableEnemies();


The class itself is registered like:

se->RegisterObjectType("Entity", 0, asOBJ_REF);
se->RegisterObjectBehaviour("Entity", asBEHAVE_RELEASE, "void f()", asMETHOD(Entity,Release), asCALL_THISCALL);

The AddRef and Release functions do nothing.

I have been using this on 64-bit Linux for many weeks with no problems, but now on every other platform that I'm trying to port it to (lin32, win32, mac64) it crashes with the failed assertion:

as_gc.cpp:273: void asCGarbageCollector::GetStatistics(asUINT*, asUINT*, asUINT*, asUINT*, asUINT*) const: Assertion `numAdded == gcNewObjects.GetLength() + gcOldObjects.GetLength() + numDestroyed' failed

I have also floundered around trying various other combinations such as registering the function signature as Entity@[]&, or making the script use Entity@[]@ with not much improvement. This is all very similar to a question I asked a while ago here, but obviously I did not completely understand the reason for the solution then, so I am sheepishly returning here again....

http://www.gamedev.net/topic/643542-gc-cannot-free-an-object-of-type-array-it-is-kept-alive-by-the-application

In that previous question, the object type was a wrapper for the real C++ class, and was registered with asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS and the script was allowed to instantiate it. I guess I could rewrite everything to do it that way again, but I was wondering if there is something I'm missing with this function registration that would let the script have a direct handle on the C++ class.

##### Share on other sites

An assert failure within the AngelScript library usually means a bug in the library rather than something you did wrong. Only in rare cases isn't AngelScript able to detect an incorrect usage so a proper error can be reported to the application. Incorrect registration of the interface is one of them, but what you've shown appears to be correct, so at this moment I think it is more likely to be a bug in AngelScript itself.

In your scenario the garbage collector somehow has an inconsistency in that it lost track of how many objects has been added to it and how many has been destroyed.

You say the crash happens on every platform except Linux64. Is assert not happening on Linux64 because you've compiled the library in release mode (i.e. the assert call is removed during the compilation)?

Is it the crash easily reproducible? Can you send me example code that reproduces the problem so that I can debug it?

What version of AngelScript are you currently using? In version 2.27.0 I added the method GetObjectInGC() to the asIScriptEngine interface. Perhaps by using that to iterate of the objects that is in the GC at different stages of your application you'll be able to figure out in what situation the assert failure occurs.

##### Share on other sites

oh... it's apology time again. I don't know why I didn't think of it before (amazing how a nights sleep makes your brain work properly again), but I had two threads touching the same script engine, without appropriate guards to prevent concurrency problems. I think the memory of that earlier thread just clouded my mind and made me jump to conclusions. Sorry for the bother!

As for why it did not occur on lin64, each lib was compiled using the cmake file with no options given, but my lin64 machine has substantially better hardware specs than the others. I guess when threading issues come into the picture, this could be relevant. Thanks again for the help!

##### Share on other sites

Can you tell me a bit more about how your two scripts were accessing the same script engine?

Though you probably should have your own guards in place to avoid two scripts updating the same data in the application, the script engine should have been able to protect its internal data against simultaneous access. Obviously there is a flaw somewhere in the garbage collector that allowed the two threads to mess up the internal data.

I'll need to find and fix this flaw, and any additional information you can provide will help.

• 13
• 18
• 29
• 11
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!