Object Referencing

Started by
11 comments, last by Khatharr 7 years, 8 months ago

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?

Advertisement

std::shared_ptr<Type> variable = std::make_shared<Type>();

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.

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.

Have you considered simply using a managed language?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

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.

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.

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.

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...

This topic is closed to new replies.

Advertisement