• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
zerochen

ref count fails

7 posts in this topic

hi,

i have following base structure
Script:
[CODE]
class A
{
};

class B
{
void init()
{
create("A");
};
};
[/CODE]

pseudo c++:
[CODE]
asIScriptObject* create(string s)
{
int id = getClassId(s);
return reinterpret_cast<asIScriptObject*>(engine->CreateScriptObject(id));
}
[/CODE]

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:
[CODE]
objB = create("B")
objB.callMethod("init")
that calls
objA = create("A")
[/CODE]

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 Edited by zerochen
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
with angelscript in c++ yes
[CODE]
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 );
[/CODE]
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.

[CODE]
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
[/CODE]

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 Edited by zerochen
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
I implemented the CreateObject function like this:

[code]
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;
}
[/code]

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?
0

Share this post


Link to post
Share on other sites
[CODE]
// 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());
}
[/CODE]

[quote]// 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 Edited by zerochen
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
ok thx that solved the problem. i thought it was because the createScriptObject func returns sometimes an obj with ref count 2
0

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  
Followers 0