Jump to content
  • Advertisement
Sign in to follow this  
AxeGuywithanAxe

Object Referencing

This topic is 811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I wanted to get some insight on how you guys handle object referencing in c++. Currently I have a base class called

CObject, and two helper classes , CObjectPtr and TObjectPtr. CObjectPtr and TObjectPtr currently act as reference count modifiers. When I assign a CObjectPtr/TObjectPtr to an object, it increases the reference count, and I can release , or assign it to null, at any point. I know some engines like UE4 and REDEngine use a garbage collector, while CryEngine/Lumberyard use a handle system. Do you think that a garbage collector is worth implementing, or is a simple smart_ptr implementation enough?

Share this post


Link to post
Share on other sites
Advertisement

Do you think that a garbage collector is worth implementing, or is a simple smart_ptr implementation enough?
 

 

You don't say what your scenario is. Is this for a game? What will you be using these objects for?

 

And using reference counting by default (your own implementation, or shared_ptr, or whatever), is a sign that you don't really understand your object lifetime or ownership semantics.

Share this post


Link to post
Share on other sites

Ah, sorry for not explaining more. "CObject" is used as the base class for all of my objects in the game that require reflection and serialization, i.e CEntity inherits from it, CEntityComponent inherits from it.. and etc. I'm not using it for every pointer, but for any pointer that has a strong ownership of an object, so like an entity has a strong ownership of it's components, so it holds a reference to it, and etc.

Share this post


Link to post
Share on other sites

Hey guys, I wanted to get some insight on how you guys handle object referencing in c++. Currently I have a base class called

CObject, and two helper classes , CObjectPtr and TObjectPtr. CObjectPtr and TObjectPtr currently act as reference count modifiers. When I assign a CObjectPtr/TObjectPtr to an object, it increases the reference count, and I can release , or assign it to null, at any point. I know some engines like UE4 and REDEngine use a garbage collector, while CryEngine/Lumberyard use a handle system. Do you think that a garbage collector is worth implementing, or is a simple smart_ptr implementation enough?

 

Sounds like shared_ptr in C++11 would work for you just fine, unless you have other special needs that your smart pointer satisfies that shared_ptr doesnt.

Share this post


Link to post
Share on other sites

I'm not using it for every pointer, but for any pointer that has a strong ownership of an object, so like an entity has a strong ownership of it's components, so it holds a reference to it, and etc.
 

 

So an entity owns its components? Have the entity manage them with a vector of unique_ptrs. There's no need for reference counting or garbage collection, since an entity will outlive its components.

Share this post


Link to post
Share on other sites

I'm not currently using a managed language because I like the low level nature of c++ and working directly with graphics apis and etc. Yeah, there are many ways to handle referencing with shared_ptr etc. My question was more of comparing a smart ptr approach , i.e shared_ptr/weak_ptr/unique_ptr , vs a garbage collection solution. Everyone here seems to be giving different examples of the smart pointer approaches, so I guess I will go with that for now.

Share this post


Link to post
Share on other sites

No need to reinvent the wheel, std::shared_ptr is enough if you want to share heap created object. You do not need garbage collector in C++ because of RAII. Use reference only if you are sure that the referenced object outlives the reference holder.

In modern c++ you should (almost) never explicitly call operator new or delete by yourself, thats what smart pointers and scopes are for. having this in mind, you should never get a memory leak.

Share this post


Link to post
Share on other sites
Ive implemented garbage collection on top of a reflection system in c++, its pretty easy to do but making it fast enough is probably a bit harder. Now days im just using reference counting and freeing thingns up manually when they die...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!