Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


- - - - -

Reference Objects as Properties


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Jason Goepel   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 August 2013 - 08:27 AM

What is the best way to expose a Reference Type as an Object Property?  
 

class TypeA  // asOBJ_REF
{
};

class TypeB // asOBJ_REF
{
    TypeA m_A;  // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
};

class TypeC // asOBJ_REF | asOBJ_GC
{
    TypeA* m_pA;  // ... RegisterObjectProperty("TypeC", "TypeA@ A", asOFFSET(TypeC, m_pA), ...
};

 
If a script gets a handle to TypeB::m_A and the TypeB object goes out of scope, it seems like m_A would be prematurely destroyed.  How does one inform the garbage collector to not destroy the TypeB object until all references to it's member m_A have been released?
 
The TypeC scenario looks more straight forward, but there is no garuntee that m_pA points to a valid object since the script could assign to it an empty handle.
 
Thanks for the help.



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
1Likes
Like

Posted 30 August 2013 - 11:39 AM

TypeB is complicated. TypeA is in this case used as a value type in C++ even though it is a reference type. Unless you implement the control to make sure TypeB stays alive as long as there are references to the member you'd do best not to expose this to the scripts at all. Instead I suggest that you use property accessors to allow the script to manipulate the TypeA member without directly accessing it.

 

For TypeC you're correct to use @. If you want to prevent that the pointer is reassigned, then ideally AngelScript should allow the member as &, but that is currently not supported. For now you'll need to use property accessors to make sure the script cannot reassign the handle. In this case you just need to expose a get accessor that returns the handle to the member.


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

#3 Jason Goepel   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 August 2013 - 01:27 PM

I'm not sure I entirely understand when the asOBJ_GC flag is needed.  Does the simple situation in my original post justify the asOBJ_GC flag?  Would the asOBJ_GC flag only be necessary if TypeA could reference TypeC?



#4 Andreas Jonsson   Moderators   -  Reputation: 3442

Like
1Likes
Like

Posted 30 August 2013 - 03:37 PM

asOBJ_GC is necessary if the object can form circular references that will not be resolved by the application.

 

The classes TypeA, TypeB, and TypeC in your example can't form any circular references, 

 

If TypeA could hold a pointer to TypeC too, then a circular reference could be formed between TypeA and TypeC. If the application doesn't keep track of these and resolve them itself then the asOBJ_GC flag and related behaviours are necessary to avoid memory leaks.


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

#5 BlackMoons   Members   -  Reputation: 441

Like
0Likes
Like

Posted 25 September 2013 - 10:19 PM

"Unless you implement the control to make sure TypeB stays alive as long as there are references to the member"

 

I wonder how one might do that.

maybe have all children add/subtract there refcount above 1 from there parent object? I had a few other ideas but this seems the simplest:

class RefCount
{
RefCount * mParent;
int mCount;
RefCount(){mCount = 1; mParent = NULL;}
Add()
{
mCount++; 
if (mParent) // We know count is above 1 or the object should of been deleted already, Add is not called for the initial 1 mCount value.
  {
  mParent->Add();
  }
}
Release()
{
if (mParent && (mCount > 1))
  {
  mParent->Release()
  }
mCount--;
if (mCount == 0) delete this;
}
};

Of course, One has to manualy set the mParent of each child 'RefType as a Value type' member in the constructor of the refTypes(your TypeB) object.

 

While I guess one could have RefCount derive from your existing ref counting class and make Add/Release virtual, I suspect the virtual call overhead would cost more then checking mParent and mCount again every release/addition. And one additional pointer per ref object is not a huge deal in most cases.

 

You would then have..

class TypeA : public RefCount // asOBJ_REF
{
};

class TypeB : public RefCount // asOBJ_REF
{
TypeA m_A; // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
TypeB()
  {
  m_A.mParent = this;
  }
};

Thoughts? Any problems with this solution?


Edited by BlackMoons, 25 September 2013 - 10:23 PM.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#6 BlackMoons   Members   -  Reputation: 441

Like
0Likes
Like

Posted 25 September 2013 - 10:22 PM

On a subnote, the below Release() might preform a little better although might not be as clear.

Release()
{
mCount--;
if (mCount > 0)
  {
  if (mParent)
    mParent->Release()
  }
else 
  delete this;
}

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com


#7 Jason Goepel   Members   -  Reputation: 442

Like
0Likes
Like

Posted 26 September 2013 - 01:32 PM

BlackMoons:

In principle that looks like it should work.  I chose to go with my "TypeC" implementation and property accessors, because the underlying C++ code would better mirror the use of the object in a script.  Your solution would avoid the extra function call involved in accessing the property, which in some scenarios would be valuable.  Thank you for your input.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS