Jump to content
  • Advertisement
Sign in to follow this  
_Vicious_

Handle to value type

Recommended Posts

Hi,

thanks for all the hard work you've done on AngelScript over the years!

Is it possible to pass around references to application-registered value types in AngelScript? For typical use case I'm returning a reference to such type and that works fine but there are also cases where I would like to be able to return a handle or a null. For such handles, the memory management is taken care of by the application.

Thanks in advance!

 

Share this post


Link to post
Share on other sites
Advertisement

No. Handles for value types is not allowed. This is because the handle potentially has a longer life time than the object. And since the value type won't be kept alive by the existence of the handle referring to it, this may lead to the handle referring to invalid memory, and potentially crash your application if accessed inappropriately.

Are you sure the object really should be registered as a value type? It sounds like you want it to behave as a reference type, so why not register it as a reference type?

 

 

Share this post


Link to post
Share on other sites

Well, the first and foremost - for simplicity's sake. I didn't really want to bother myself with coding the reference counting machinery because objects of this particular POD type aren't created via the factory pattern. They are either attached to game entities, which are pool-allocated and have infinite lifetime or are temporary stack objects used to pass the entity state around.

Share this post


Link to post
Share on other sites
Posted (edited)

Please correct me if I'm wrong but it just occurred to me that what I had described above sounds like a potential use-case for asOBJ_NOCOUNT handle types but not quite, as I would still like AS to perform the reference counting on script objects.

Edited by _Vicious_

Share this post


Link to post
Share on other sites

Yes. asOBJ_NOCOUNT will allow you register reference types without having to bother with implementing reference counting. It works well for cases like you mentioned where the object instance has infinite lifetime.

However, it appears you use two distinct memory models for the same type. You don't want to allow handles to the instances that are supposed to be temporary stack objects, as the handle might stay alive longer than the object on the stack. And you don't want to use instances from the memory pool as temporary stack objects as it would be difficult to know when the pooled instance can be reused.

To solve this you could register the type twice with different names. Once for the pool allocated model as asOBJ_REF|asOBJ_NOCOUNT, and another for the temporary stack objects as asOBJ_VALUE|asOBJ_POD. Then add implicit conversions between the two so that it when passing the objects around by value they can be interchanged transparently.

 

 

Share this post


Link to post
Share on other sites

Thanks for your suggestion but the idea of having two identical classes felt somewhat clumsy. I ended up converting the class to use reference counting, which is only performed for script-allocated objects, looked up via std::map with pointer as the key. This incurs performance penalty but hopefully it's going to be negligible .

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  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!