Hello GDNet. I am studying the SDL library and while doing so i find myself in troubles back in the C++ basics.
The question is : If you delete an object that is dynamically allocated, and then try it again does it cause problems or leaks ?
The issue is as follows:
I have a Sprite class which basically deals with loading sprites and drawing them onto the screen.
I have Game class with functions like Init() Draw() Update() Quit().
I dynamically create new sprites in the Game class for each image i want to draw, and then i delete objects in the Quit() function.
In the main game loop i experimented with some things. I attached a key event to delete specific sprite object which actually removes the
sprite from the screen and clears the allocated memory. Note that i did that in the game loop. And in the Quit function i also delete
that object. Is this ok ? It does not seem to crash the program or anything but what happens behind the scenes ?
This can be an issue when the Player kills an Enemy and i free that specific object, and then back in the Quit function i try to
free the garbage again.
Here is some code for reference
game.cpp
Sprite Background = new Sprite();
Game::Initialize()
{
Background->LoadSprite("bg.bmp");
/........../
}
Game::Loop()
{
while(Running){
while(PollEvent){
if(KEY == "x"){
delete Background;
Background = NULL;
}
}
}
}
Game::Quit()
{
//Background once deleted and now again ?
delete Background;
}