after learning that i cant pass objects as pure pointers, i chose to use any ad-don.
i have an hashmap implementation like this,
r = engine->RegisterObjectType("hashmap2D", sizeof(hashmap2D), asOBJ_VALUE | asOBJ_APP_CLASS_C); assert( r >= 0 );
r = engine->RegisterObjectMethod("hashmap2D", "any @opIndex(const Vector2 &in)", asFUNCTION(hashmap2D::hashmap2DopIndex), asCALL_CDECL_OBJLAST) ; assert( r >= 0 );
r = engine->RegisterObjectMethod("hashmap2D", "any @Get(const Vector2 &in)", asMETHOD(hashmap2D, Get), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("hashmap2D", "void Set(const Vector2 &in, any @value)", asMETHOD(hashmap2D, Set), asCALL_THISCALL); assert( r >= 0 );
Destructor of hashmap2D is this. it calls release on all any objects
// this is called only once. when engine->Release(); called
for(Hash2D::iterator it = values.begin(); it != values.end();)
{
it->second->Release(); // refCount is -17891602, any is already destroyed, but why?
it = values.erase(it);
}
Set function
void Set(const Vector2 &pos, CScriptAny *obj)
{
obj->AddRef(); // addRef since i am storing it
values[pos] = obj;
}
get function
CScriptAny *Get(const Vector2 &pos)
{
if(!values.count(pos))
return 0;
return values[pos];
}
[ ] implemented like this
static CScriptAny * hashmap2DopIndex(const Vector2 &index, hashmap2D *self)
{
if( self->values.count(index) )
{
return self->values.at(index);
}
return 0;
}
script function calling [ ] operator is this
hashmap2D map; // global
void SomeFunc()
{
Vector2 pos(1,1);
any coord;
coord.store(Vector2(54, 84));
//refCount of any is 3
map.Set(pos, coord);
//refCount of any is 4 ( me + GC + SomeFunc + ?? ) // who owns 4th ref?
any coord2;
//refCount of any is 4
coord2 = map.Get(pos);
//refCount of any is 4
Vector2 stored;
coord2.retrieve(stored);
Vector2 v;
//refCount of any is 3, ???? why did it decrease?
any va = map[pos];
//refCount of any is 3
va.retrieve(v);
ConsoleOutput("Stored vector x: " + v.x + " y: " + v.y); // value accessed correctly
}
During program termition, it ends with a Heap corruption.
it seems i am over destroying it, but hashmap2D destructor is called only once.
it does not cause heap when i just call only [ ] or only Get. it does crash when i call both.
i am very confused on how hashmap2D is destroyed.
i hope i made the issue clear enough.
thank you.