• 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
Dekowta

Ref counting

7 posts in this topic

I have a slight problem which I have a solution to but want to check to see if I need to do it.

I have a structure that inherits from another structure and I need the cast ability in angel script to typecast the inherited object to the base class.
However this requires the object to be a reference however I would rather the object be of value type since its only a structure to send to a function to create another object.

If i created the class with no ref counter I take I would have to delete the object within C++ and ensure the angel script object/s is also cleaned up as well.

I can still inherit and add a ref counter but I would rather not have to do this.

Thanks

0

Share this post


Link to post
Share on other sites

With asOBJ_NOCOUNT you're telling AngelScript the objects of the type will be allocated on the heap just like any other reference type but that AngelScript doesn't need to increment and decrement the references. All the rules that apply to asOBJ_REF are also valid for asOBJ_REF with asOBJ_NOCOUNT so they will allow the ref cast behaviour.

 

Of course without the reference counting you'll be responsible for identifying when no more references to the object exists and the memory can be released.

0

Share this post


Link to post
Share on other sites

I take it by that then if I was to go

obj@ OBJA = obj();

obj@ OBJB = OBJA;

 

with ref counting the object OBJA will have a ref count of 2. If I was to make OBJA NULL I take it the ref count of the object will go down to 1 and OBJB will still be valid until it comes out of scope or is equal to NULL?

If I didnt use ref counting and I made OBJA NULL does OBJB still exist and I would have to explicitly make it equal to NULL to also remove it even if OBJB went out of scope.

Sorry if this is all a bit basic I have a rough understanding of it but I just want to make sure im not doing anything that will cause memory leaks and corruptions.

0

Share this post


Link to post
Share on other sites
obj() creates the object instance that is allocated somewhere on the heap.

OBJA and OBJB are both handles that refer to the same object instance. And as long as there are any references the object instance should not be destroyed and deallocated, or you'll end up with what's often called "dangling pointers". Access to an invalid object through such dangling pointers can cause unexpected behaviour, in the best case it will immediately crash the application, but in a worst case it will cause less noticeable bugs that can be very difficult to find.

With reference counting the obj knows exactly how many handles are referring to it, and can thus easily know when it is safe to destroy and deallocate the object.

Without reference counting you'll have a difficult time to know how many handles there are. But with careful design of your application you'll be able know when it is not possible to exist any handles, and the object can be destroyed, e.g if there are no active script contexts and there are no global variables in the scripts you know that the script can't hold any references to the object.
0

Share this post


Link to post
Share on other sites

How then would I go about implementing reference counting on a C++ object I don't have access to adding a reference counter directly to the object?
 

0

Share this post


Link to post
Share on other sites

There are a many different possibilities.

 

One is to create a thin wrapper structure, like what I've done with the std::string class here:

 

http://svn.code.sf.net/p/angelscript/code/trunk/sdk/tests/test_feature/source/scriptstring.h

http://svn.code.sf.net/p/angelscript/code/trunk/sdk/tests/test_feature/source/scriptstring.cpp

 

Another is to keep the ref counter in a lookup map. For example something like this:

 

std::map<Object*, int> refCounters;
void Object_AddRef(Object *obj)
{
   std::map<Object*,int>::iterator it = refCounters.find(obj);
   if( it != refCounters.end() )
     it->second++;
   else
     refCounters.insert(std::map<Object*,int>::pair(obj, 1);
}
void Object_Release(Object *obj)
{
   std::map<Object*,int>::iterator it = refCounters.find(obj);
   if( it != refCounters.end() )
   {
     if( --(it->second) == 0 )
     {
       delete obj;
       refCounters.erase(it);
     }
   }
}
// Register these with AngelScript like this:
engine->RegisterObjectBehaviour("Object", asBEHAVE_ADDREF, "void f()", asFUNCTION(Object_AddRef), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("Object", asBEHAVE_RELEASE, "void f()", asFUNCTION(Object_Release), asCALL_CDECL_OBJLAST);

 

Please forgive coding errors, I'm writing this from memory.

 

The first alternative is probably more performatic, but has the drawback that you're actually using a different object type than the true type. In many cases this will not be a problem, but in others you'll need to wrap your application functions to translate the real object into the wrapper object so it will have the reference counter.

 

The second alternative should work pretty seamlessly, though the performance might suffer somewhat if you have a lot of operations on this object as the look-up into the map will add significant overhead. 

0

Share this post


Link to post
Share on other sites

Thanks for the reply.

Sorry didn't reply sooner thought I had done already.

Seems to have worked fine though the object still seems to exist after the ref count reaches 0 and the object is deleted. For example:
 

 

Aclass@ myclass = Aclass();

void initalise()
{
    Print("init");
    myclass.printA();
}

void update()
{
    myclass.printA();
}
 


The add ref function is called and then the remove ref before the initalise function is called yet the myclass object works as intended and now my map has no objects stored.

not really sure what is going on though im sure im doing something wrong.


The class is registered as followed
 

AngelManager::registerClass("Aclass", 0, asOBJ_REF);
AngelManager::registerClassBehaviour("Aclass", "Aclass@ f()", asBEHAVE_FACTORY, asFUNCTION(createA));
AngelManager::registerClassBehaviour("Aclass", "void f()", asBEHAVE_ADDREF, asFUNCTION(ObjectA_AddRef), asCALL_CDECL_OBJLAST);
AngelManager::registerClassBehaviour("Aclass", "void f()", asBEHAVE_RELEASE, asFUNCTION(ObjectA_Release), asCALL_CDECL_OBJLAST);
AngelManager::registerClassMethod("Aclass", "void printA()", asMETHOD(Aclass, printA));


 

0

Share this post


Link to post
Share on other sites

You need to make sure the object is added to the map with the factory function, i.e. when the factory function returns the handle the refCount should be at least 1 (it can be higher if you're storing the handle elsewhere too).

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