Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Destructor design in C++ for object destruction/deletion [Tier 2]

Author
Message
Daqs
9
Years of Service
User Offline
Joined: 23rd May 2015
Location:
Posted: 30th May 2015 14:00
Before I start my question, here's my simple code:

//File.h


File.cpp


template.cpp



Questions: How do I destroy the text? Where do I call the destructor? in which function? (loop or end or somewhere else?).

Do I even call the destructor or does it get called automatically and delete the text so that there is no memory leak?

I understand the concept of constructors/destructors. My confusion is in terms of having functions like Begin(), Loop(), End() and where and how should I exactly be deleting objects after they are created.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th May 2015 19:40
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:



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!
Daqs
9
Years of Service
User Offline
Joined: 23rd May 2015
Location:
Posted: 31st May 2015 05:29
Thank you!

Login to post a reply

Server time is: 2024-11-16 18:19:51
Your offset time is: 2024-11-16 18:19:51