   Memory Leak Problem
  Hi everyone,I have a memory leak in one of my programs, and I can't seem to figure out where, I have spent about an hour looking for the reason, but I just can't seem to find it. Any help would be appreciated.Here is the code:Mouse* mouse = new Mouse();Background* background = new Background();Sprite* button = new Sprite();vector<Sprite*> buildings;Sprite* building = new Sprite();void Application::handleInput(){static UCHAR keyBuffer[256];if (!GetKeyboardState(keyBuffer)){MessageBox(NULL, L"Error setting up keyboard", L"Error", MB_OK);return;}if (keyBuffer[VK_ESCAPE] & 0xF0){PostQuitMessage(0);}RECT buttonRect = {(int)button->getX(), (int)button->getY(), (int)button->getX() + (int)button->getWidth(), (int)button->getY() + (int)button->getHeight()};if (getLeftClick() && isOver(buttonRect)){mouse->setFrame(1);mouse->setWidth(70.0f);mouse->setHeight(70.0f);}if (mouse->getFrame() != 0){if (getRightClick()){building = new Sprite();building->set(0.0f, 0.0f, 70.0f, 70.0f);building->createTexture();building->setX(mouse->getX());building->setY(mouse->getY());buildings.push_back(building);}}}void Application::setSprites(){mouse->set(0.0f, 0.0f, 25.0f, 25.0f);mouse->createTextures();background->set(0.0f, 0.0f, 1024.0f, 1024.0f);background->createTextures();button->set(100.0f, 900.0f, 70.0f, 70.0f);button->createTexture();building->set(button->getX(), button->getY(), 70.0f, 70.0f);building->createTexture();}void Application::deleteAllocatedMemory(){for (int i = 0; i < buildings.size(); i++){delete buildings;}delete button;delete background;delete mouse;}The everything worked until I added the vector. I was able to free all of the memory before, but now the output from the debugger says:Total Memory Unfreed From Current Process = 1218831 bytesAgain, any help would be appreciated.   
building = new Sprite();building->set(0.0f, 0.0f, 70.0f, 70.0f);building->createTexture();building->setX(mouse->getX());building->setY(mouse->getY());buildings.push_back(building);I just did a quick look up on Vectors, and I think you should do it this way.You should make a temporary sprite first.Sprite * tempBuilding = new Sprite;tempBuilding->set(0.0f, 0.0f, 70.0f, 70.0f);tempBuilding->createTexture();tempBuilding->setX(mouse->getX());tempBuilding->setY(mouse->getY());buildings.push_back(tempBuilding);I don't think it's working the way you intended the way you have it written. It looks like you are just allocating new memory to your building and pointing to that, and just losing the old location and never deallocating it or even able to get at it.   ;  Yeah, this is my first real attempt at using vectors. I've done some dynamic memory before but not much, so...yeah, it probably doesn't do what I want it to.[smile]With your suggestion, how/when do I free the memory for tempBuilding?   ;  tempBuilding doesn't have any memory, it has an address to the new memory. When you add it to the end of the vector, the vector has that address now, and it's the memory you free later in your deallocation function. tempBuilding is just a local pointer that will only exist in that scope of the if check, so it doesn't need anything done to it.   ;  Sorry, I just tried your new code and it didn't work. Do you have any other ideas? Thanks for your help so far.   ;Mouse* mouse = new Mouse();Background* background = new Background();Sprite* button = new Sprite();vector<Sprite*> buildings;Sprite* building = new Sprite();void Application::deleteAllocatedMemory(){for (int i = 0; i < buildings.size(); i++){delete buildings;}delete button;delete background;delete mouse;}You allocate memory for building, but you don't seem to have freed it anywhere.  Try addingdelete building; to your deleteAllocatedMemory function.   ;  C++ allows you to do many things, some of which are elegant, and some of which are manually managing your memory. This leak that you have now is probably because you never delete building. You can solve this leak for now, but there will be others, because your coding style is unsafe. Using new and delete is dangerous and should only be approached with caution! Try declaring objects and letting them fall out of scope, instead of declaring pointers to objects and managing their existence yourself. Try Standard Library containers instead of creating and recreating an array with new[] and delete[]. Try the auto pointers and pointer containers in boost.Dont do this!Mouse* mouse = new Mouse();Background* background = new Background();Sprite* button = new Sprite();vector<Sprite*> buildings;Sprite* building = new Sprite();;  The problem lies here:vector<Sprite*> buildings;Sprite* building = new Sprite();You new added the new building to the vector, and then later on do a new Sprite() again, never taking care of your previously created building.Change that line to Sprite *building = NULL; and it should be solved. However, make sure to not reference the building to render it etc. at this point.Toolmaker   ;  Also is there a specific reason you are allocating mouse keyboard etc on the heap with new?If not just make them normal members it will save a lot of troubles.I also suggest looking into shared_ptr it handles freeing of memory for you and multiple references.Using it with std::vector is easy too.std::vector<boost::shared_ptr<Sprite>> mySprites;boost::shared_ptr<Sprite> newSprite(new Sprite(params,position,color,etc..));mySprites.push_back(newSprite);//no need to call delete the sprites will be freed automatically//you can also hand out references to sprites and not worry about dangling pointers;Quote:Original post by Black Knightstd::vector<boost::shared_ptr<Sprite>> mySprites;Small correction:std::vector<boost::shared_ptr<Sprite> > mySprites;