简体   繁体   中英

c++ game engine design and objects going out of scope

I'm working on a small component based game engine in C++. Various components are added to an internal list of a gameobject object, to save some time I don't make a member variable of these components, only the game object is a member variable, like so:

void Initialize()
{     
   MeshComponent* meshRenderer = new MeshComponent(mesh, material);
   m_GameObject.AddComponent(meshRenderer);
}

The meshRenderer variable is being added to a component list in AddComponent(), but goes out of scope at the end of this function.

Later on (in the game's update/draw loops) this component is called and everything works, despite the object having gone out of scope during initialization.

Do I understand something wrong about scope, is it safe to use like this or is there another approach I should take (without having to make member variables of each of my components)?

Thanks for the help!

First of all, I suggest you to study C++ more before starting big projects like game engine.

What goes out of scope is the meshRenderer variable that is of type MeshComponent* , that is pointer to MeshComponent . What happens when pointer goes out of scope? Nothing at all. The memory was allocated on heap using the new operator and it will stay there until you deallocate it using delete operator.

Later on (in the game's update/draw loops) this component is called and everything works, despite the object having gone out of scope during initialization .

That because in the block:

void Initialize()
{     
   MeshComponent* meshRenderer = new MeshComponent(mesh, material);
   m_GameObject.AddComponent(meshRenderer);
}

The variable in the stack is the pointer ( meshRenderer ), the pointed object is allocated on heap . So when } is reached only the pointer is "destroyed" but not the pointed object.

This is one of mainly cause of error with dynamic memory.

So it should work "correctly" until the components manager handles correctly the dynamic memory (eg proper deallocation).


is it safe to use like this or is there another approach I should take (without having to make member variables of each of my components)?

Safe a relative concept. Even someone with a great experience in C or C++ dynamic memory could make some mistakes producing memory leaks .

I can suggest you to read and to learn smart pointers . [ since C++11 ]

A smart pointer simplify the concept of pointer because (if correctly used) it handles automatically the memory deallocation.

In your case, for example, an std::shared_ptr could be useful.

A shared pointer allows multiple instance of pointers which refer the same object (the same memory). Indeed, several std::shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when the last remaining shared_ptr owning the object is destroyed (or the programmer explicit wants).

Later on (in the game's update/draw loops) this component is called and everything works, despite the object having gone out of scope during initialization.

That component doesn't go out of scope unless being delete d somewhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM