ref count fails

Started by
6 comments, last by zerochen 11 years, 6 months ago
hi,

i have following base structure
Script:

class A
{
};

class B
{
void init()
{
create("A");
};
};


pseudo c++:

asIScriptObject* create(string s)
{
int id = getClassId(s);
return reinterpret_cast<asIScriptObject*>(engine->CreateScriptObject(id));
}


so now to my problem.

i create class B and than i call the function init() from B that creates a new object from A
something like this:

pseudo call stack:

objB = create("B")
objB.callMethod("init")
that calls
objA = create("A")


after that the ref count of objB is 1 (as expected)
the ref count of objA is 2 (have to be 1)

have i an error in my logic?

i tracked this down to:
as_scriptobject.cpp line 122
that line doesnt dec the ref counter of objA but it does it for objB.

is that because to call a script function there has to be an active context?

is this a bug or have i call the release func myself?

thx for help
zerochen
Advertisement
I don't see anything wrong with what you're doing. It might be a problem with the nested context functionality in AngelScript.

How have you registered the create() function with AngelScript?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

with angelscript in c++ yes

CScriptHandle CScriptFunction::createObject(const std::string& name, const core::vector3df& pos)
r = script->getEngine()->RegisterGlobalFunction("ref@ CreateObject(const string& in, const vector3df& in)", asMETHODPR(CScriptFunction, createObject, (const std::string&, const core::vector3df&), CScriptHandle), this, asCALL_THISCALL); assert( r >= 0 );

i wrote a patch for angelscript that allows me to register a global script function as a method of a class.
maybe thats the problem but i use it for weeks without any problems. i think it is the nested context
here is the patch file.


Index: include/angelscript.h
===================================================================
--- include/angelscript.h (Revision 1359)
+++ include/angelscript.h (Arbeitskopie)
@@ -520,6 +520,7 @@
virtual asIJITCompiler *GetJITCompiler() const = 0;

// Global functions
+ virtual int RegisterGlobalFunction(const char* declaration, const asSFuncPtr &funcPointer, void* obj, asDWORD callConv) = 0;
virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv) = 0;
virtual asUINT GetGlobalFunctionCount() const = 0;
#ifdef AS_DEPRECATED
Index: source/as_callfunc.cpp
===================================================================
--- source/as_callfunc.cpp (Revision 1359)
+++ source/as_callfunc.cpp (Arbeitskopie)
@@ -409,12 +409,8 @@

if( callConv >= ICC_THISCALL )
{
- if( objectPointer )
+ if(!((obj = objectPointer) || (obj = descr->GetUserData())))
{
- obj = objectPointer;
- }
- else
- {
// The object pointer should be popped from the context stack
popSize += AS_PTR_SIZE;

Index: source/as_scriptengine.cpp
===================================================================
--- source/as_scriptengine.cpp (Revision 1359)
+++ source/as_scriptengine.cpp (Arbeitskopie)
@@ -2458,6 +2458,83 @@
}

// interface
+int asCScriptEngine::RegisterGlobalFunction(const char* declaration, const asSFuncPtr &funcPointer, void* obj, asDWORD callConv)
+{
+ asSSystemFunctionInterface internal;
+
+ int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
+ if( r < 0 )
+ return ConfigError(r, "RegisterGlobalFunction", declaration, 0);
+
+ if( callConv != asCALL_THISCALL)
+ return ConfigError(asNOT_SUPPORTED, "RegisterGlobalFunction", declaration, 0);
+
+ isPrepared = false;
+
+ // Put the system function in the list of system functions
+ asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface)(internal);
+
+ if( newInterface == 0 )
+ return ConfigError(asOUT_OF_MEMORY, "RegisterGlobalFunction", declaration, 0);
+
+ asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
+
+ if( func == 0 )
+ {
+ asDELETE(newInterface, asSSystemFunctionInterface);
+ return ConfigError(asOUT_OF_MEMORY, "RegisterGlobalFunction", declaration, 0);
+ }
+
+ func->sysFuncIntf = newInterface;
+ func->SetUserData(obj);
+
+ asCBuilder bld(this, 0);
+ r = bld.ParseFunctionDeclaration(0, declaration, func, true, &newInterface->paramAutoHandles, &newInterface->returnAutoHandle, defaultNamespace);
+ if( r < 0 )
+ {
+ // Set as dummy function before deleting
+ func->funcType = asFUNC_DUMMY;
+ asDELETE(func,asCScriptFunction);
+ return ConfigError(asINVALID_DECLARATION, "RegisterGlobalFunction", declaration, 0);
+ }
+
+ // TODO: namespace: What if the declaration defined an explicit namespace?
+ func->nameSpace = defaultNamespace;
+
+ // Check name conflicts
+ r = bld.CheckNameConflict(func->name.AddressOf(), 0, 0, defaultNamespace);
+ if( r < 0 )
+ {
+ asDELETE(func,asCScriptFunction);
+ return ConfigError(asNAME_TAKEN, "RegisterGlobalFunction", declaration, 0);
+ }
+
+ func->id = GetNextScriptFunctionId();
+ SetScriptFunction(func);
+
+ currentGroup->scriptFunctions.PushLast(func);
+ func->accessMask = defaultAccessMask;
+ registeredGlobalFuncs.PushLast(func);
+
+ // If parameter type from other groups are used, add references
+ if( func->returnType.GetObjectType() )
+ {
+ asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
+ currentGroup->RefConfigGroup(group);
+ }
+ for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
+ {
+ if( func->parameterTypes[n].GetObjectType() )
+ {
+ asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
+ currentGroup->RefConfigGroup(group);
+ }
+ }
+
+ // Return the function id as success
+ return func->id;
+}
+
int asCScriptEngine::RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv)
{
asSSystemFunctionInterface internal;
Index: source/as_scriptengine.h
===================================================================
--- source/as_scriptengine.h (Revision 1359)
+++ source/as_scriptengine.h (Arbeitskopie)
@@ -100,6 +100,7 @@
virtual asIJITCompiler *GetJITCompiler() const;

// Global functions
+ virtual int RegisterGlobalFunction(const char* declaration, const asSFuncPtr &funcPointer, void* obj, asDWORD callConv);
virtual int RegisterGlobalFunction(const char *declaration, const asSFuncPtr &funcPointer, asDWORD callConv);
virtual asUINT GetGlobalFunctionCount() const;
#ifdef AS_DEPRECATED


i just copied the RegisterGlobalFunction, added the void* obj pointer to the function and set it after

func->sysFuncIntf = newInterface;
+ func->SetUserData(obj);

ah i use the lastest angelscript svn version of couse
Your modification to the engine to allow registration of a class method as a global function doesn't appear to be the cause of the problem.

I see you're using the CScriptHandle add-on to return the newly created object. That might have something to do with the problem.

Do you mind showing how the CScriptFunction::createObject method is implemented? At least the part from the call to asIScriptEngine::CreateScriptObject to the return of the CScriptHandle? There might be something there that is causing the problem.

In meantime I'll try to reproduce the problem with what you've already shown.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I implemented the CreateObject function like this:


static CScriptHandle CreateObject(const string &s)
{
asIScriptContext *ctx = asGetActiveContext();
asIScriptEngine *engine = ctx->GetEngine();
asIScriptModule *mod = engine->GetModule("test");
asIObjectType *type = mod->GetObjectTypeByName(s.c_str());


// The object will start with ref count == 1
asIScriptObject *obj = reinterpret_cast<asIScriptObject*>(engine->CreateScriptObject(type->GetTypeId()));

// Put the object in the generic handle container for return
CScriptHandle ref(obj, type);


// The CScriptHandle increased the ref count when it
// stored the handle internally so we need to release one
obj->Release();

return ref;
}


I don't get any wrong reference counts when the function is implemented like this.

Do you release() the ref count after putting the object into the CScriptHandle?

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 function is called from angelscript
CScriptHandle CScriptFunction::createObject(const std::string& name, const core::vector3df& pos)
{
int id = builder.GetModule()->GetTypeIdByDecl(name.c_str());
if(id == asINVALID_TYPE)
{
//error msg
return CScriptHandle();
}

asIScriptObject* s = reinterpret_cast<asIScriptObject*>(engine->CreateScriptObject(id));
//here the ref count is sometimes 2

if(!scriptObj)
return CScriptHandle();

// set pos
// add it to an objArray

return CScriptHandle(scriptObj, scriptObj->GetObjectType());
}


// The CScriptHandle increased the ref count when it
// stored the handle internally so we need to release one[/quote]

yes but if the CScriptHandle will be destroyed the ref counter will dec

but the problem is direct after the engine createObject function so i think it is a angelscript problem

i will write a little example that reproduce the problem but now i have no time sry
The ref count after CreateScriptObject() can be 2 if the object requires garbage collections. What happens then is that the object notifies the garbage collector of its existence, which will increment the ref counter.

Anyway, in your implementation of CScriptFunction::createObject() you need to release the reference after creating the CScriptHandle that will be returned.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

ok thx that solved the problem. i thought it was because the createScriptObject func returns sometimes an obj with ref count 2

This topic is closed to new replies.

Advertisement