This question relates to C++ game engine, called AppGameKit (AGK).
I've created a separate class for Text so that I don't have to call AGK functions when creating Text. Here's the simple class:
Text.h
class Text
{
private: int _ID;
void Destory();
public:
void AddText();
Text(int ID);
~Text();
};
Text::Destroy()
{
agk::DeleteText(_ID);
}
Text::~Text()
{
Text::Destroy();
}
Now my question is when I'm calling this class in any other class, say MainMenu, do I have to delete the button in the class MainMenu that I'm creating using this class or will the destructor of Text will automatically get called and delete the button.
MainMenu.cpp
MainMenu::Initilization()
{
Text * mainText = new Text(1);
}
MainMenu::Destory()
{
agk::DeleteText(1); // DO I HAVE TO DO THIS?
}
MainMenu::~MainMenu()
{
MainMenu::Destory();
}
AGK function delete is called to delete text so that the memory is deallocated. Similar to C++ delete keyword.
Personally, I think deleting the button in MainMenu class should be unnecessary but I'm confused as to whether the destructor of Text class is even called. Please let me know if you think I'm wrong.
Every
new
has to be balanced with adelete
else you will leak memory. (You can use classes likestd::unique_ptr
which will manage the deletion for you but they still calldelete
under the hood).Currently,
mainText
goes out of scope at the end of theInitilization
function, so you lose the pointer that you need for a successfuldelete
.