I am trying to make sure that I delete all memory allocations from the new command, ran into 1 problem and I'm not sure If I understand when how the delete in a destructor of a class will be called.

Problem: I create a class in main, after the game loop I call the delete class just before return 0, it locks the program, when I coment it out, shuts down fine.

Coming form java, I have an understanding of delete and new, but I'm sure it works different in c++, when is a destructor of a class called? For example, I creating a vector of pointers to a class, this class will conatain image, and some data, in that destructor I'm going to delete the image, but I don't think it would be a good Idea to put a delete this in the destructor, is that correct? I'm assuming that when I pop a vector, that will make the pointer no longer valid, is this where the destructor gets called?

guess i'll stop here with the questions, maybe answers to some of the above will clear others up.

thanks in advance.

when is a destructor of a class called?

When the object goes out of scope or a pointer to the object is deleted.

I'm assuming that when I pop a vector, that will make the pointer no longer valid, is this where the destructor gets called?

Yes, assuming that the object stored in the vector is an object and not a pointer to an object that was dynamically allocated. If you use new or new[], the onus is on you to call delete at the right time.

It's hard to say why your code is locking up without seeing a relevant skeleton.

this is the code, currentLevel contains the pointer down at the bottom I delete it.

   Level *currentLevel = new Level("data/level1.txt");

   while (displayWindow.isOpen()) {

      sf::Event event;
      while (textureWindow.pollEvent(event)) {
         if (event.type == sf::Event::Closed)
            textureWindow.close();
         if (event.type == sf::Event::MouseEntered) {
            std::cout << "Mouse entered" << std::endl;

         }
      }

      while (displayWindow.pollEvent(event)) {
         if (event.type == sf::Event::Closed)
            displayWindow.close();
      }

      displayWindow.clear();
      textureWindow.clear();

      displayWindow.draw(text);

      displayWindow.display();
      textureWindow.display();
   }


   if(currentLevel) delete currentLevel;
   return 0;
}

class decs:
Level::Level(std::string path) {

   std::cout << this->mapSize << std::endl;

   loadFromFile(path);   
   this->mapSize = 17;
   prevLevelFile = "";
   levelFile = path;
   levelMap = new LevelMap(mapFile, mapSize);
}

Level::~Level() {
   delete levelMap;
   delete terrain;
}

void Level::Draw(sf::RenderWindow window) {

}

void Level::Update() {

}

bool Level::loadFromFile(std::string path) {
   prevLevelFile = levelFile;
   levelFile = path;
   std::ifstream levelFile(path.c_str());
   std::string line;

   if(!levelFile.is_open()) {
      std::cout << "could not open " << path << std::endl;
      return false;
   }
   int index = 0;
   while(std::getline(levelFile,line)) {
      if(index == 0) {
         mapSize = atoi(line.c_str());
      }
      else if(index == 1) {
         spriteFile = line;
      }
      else if(index == 2) {

         mapFile = line;
      }
      index++;
   }
   levelFile.close();
   return true;
}

#include "LevelMap.h"
#include "SpriteSheet.h"
#include <SFML/Graphics.hpp>
#include <sstream>

class Level {
public:
   Level(std::string path);
   virtual ~Level();
   void Draw(sf::RenderWindow window);
   void Update();
   bool loadFromFile(std::string path);

private:
   LevelMap *levelMap;
   SpriteSheet *terrain;
   std::string levelFile,prevLevelFile, mapFile, spriteFile;
   int mapSize;

};

The problem is coming from the fact that you delete the terrain pointer in the destructor, but that pointer points nowhere, i.e., you have not created an object to which it points to using the new operator. You can only call delete on pointers that point to memory allocated with new, which isn't the case here.

Also, your code is dangerous because you have not defined the copy-constructor and assignment operator. Read this tutorial.

And in modern C++, you should never really need to use delete, if you follow the guidelines in this tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.