As fastcall22 mentioned, only
delete what you
new - and if you're a beginner, don't new or delete at all. And if you're a professional, and using modern C++ techniques, you'd use smart pointers 95% of the time anyway so you'd still mostly ignore new and delete.
C++ doesn't have a garbage collector, what are you talking about?
This only proves to me that my C++ teacher wasn't that great, he never clarified certain things so I was left to assume.
It's the student's job to ask questions if they don't understand.
It's the teacher's job to answer questions - that's what they are there for (otherwise, you might as well just read a book).
The teacher can't answer questions that the students don't ask.
Plus, C++
does automatically collect some memory. It doesn't have a "garbage collector", but it still frees memory automatically in certain circumstances. Maybe your teacher was trying to explain how C++ memory was collected, and having heard the phrase "garbage collector" sometime in the past, you might've automatically assumed that the teacher was talking about garbage collection?
I'm guessing this is the case, because this is exactly the problem you are having with your code: You think certain memory isn't getting freed that C++ is already freeing for you, and you're trying the manually free memory that has already been freed (or that will automatically be freed later).
As popular as it is to do so, don't assume your teacher is ignorant. Just ask questions if you don't understand. If, after asking questions, something still doesn't make sense, then ask on these forums and we can clarify.
Here's how basic C++ variables work: (Don't roll your eyes, this is important to understand!)
int x = 0;
That line of code creates an integer named 'x', that is initialized to '0'. It will
automatically be freed when the execution of the code reaches the end of its scope.
Example:
void func()
{
int a = 20;
if(true)
{
int b = 70;
} //The variable 'b' gets freed after this point.
int c = 0;
} //The variables 'a' and 'c' get freed after this point.
If you do this:
void func()
{
int a = 20;
delete &a; //Programmer manually frees 'a' (not good).
} //Compiler automatically frees 'a'. But since was *already* freed, the program will either
//crash (if you're lucky) or do unexpected crazy stuff that's really hard to track down and debug.
For every variable in your code, either you're managing its memory, or the compiler is. But not both. Every variable should have an owner: Who owns it? The function? The class? Or are you manually managing it?
Now let's look at the lifetime of variables in structs: (classes behave the same way)
struct MyStruct
{
int x;
};
'x' is owned by 'MyStruct'. When 'MyStruct' has its memory freed, 'x' will also be freed.
Observe:
void func()
{
MyStruct myStruct;
myStruct.x = 20;
} //'myStruct' is freed here. Because 'myStruct' is freed, 'myStruct.x' is also freed at the same time.
Now classes also have constructors and destructors, which is important to use. All your '
Load()' code should go in the class's constructor. That's what it's for.
All your '
TidyUp()' code should go in the class's destructor. That's why destructors exist.
//This is a constructor. It's a function called when a class gets created.
MainMenuState::MainMenuState()
{
}
//This is a destructor. It's a function called when a class gets destroyed.
MainMenuState::~MainMenuState()
{
}
Unless you're manually doing something that needs to be manually undone, you don't really need to use a destructor in this MainMenuState class.