In C++ the destructor is called automatically either when:
- if the object is on the stack, it gets popped off the stack (goes out of scope)
- if the object is on the heap (allocated with 'new' keyword), it is deleted with the 'delete' keyword.
Now to tackle the agk::Loop().
As you'll mainly be working with pointers, you need to have sufficient NULL checks in place.
So in your example:
File* _file;
void app::Begin(void)
{
_file = new File(); // calls the constructor
}
void app::Loop(void)
{
if(condition_For_Destroying_Text)
{
if(_file != NULL) delete _file; // calls destructor
_file = NULL;
}
}
void app::End(void)
{
// if the text is still allocated while app is terminating
if(_file != NULL) delete _file; // calls destructor
}
As you're new to C++ here are a few tips:
- try to keep logic to a minimum in constructors/destructors. This includes trying not to use the agk commands inside constructors as creations may fail and constructors are always called when an object is created.
- don't go crazy over memory leaks. Yes it's important to manage memory correctly however this will take time to learn especially in the context of AGK. If you accidentally leak memory - it's not the end of the world but do keep a sharp eye out for it.
- With tackling the app::Loop(), have your own Start(), Loop() functions for the appropriate objects - this way you can control what's called in the loop.
Happy programming!