Hello guys,
Everytime I try to delete the font variable from memory I get the error "Heap corruption detected. CRT detected that application wrote to memory after end of heap buffer". I realized that when I comment the loadFromFile() method on MainGame.cpp, I don't get the error anymore. I searched on google how to solve the problem and I found some posts that say it happen when you allocate less memory than you really need. As the problem is occurring only with the loadFromFile() method, I came to this forum ask for help.
P.S.: I am using Visual Studio Community 2015. 6GB RAM Memory.
MainGame.h#pragma once
#include "GameState.h"
#include "MainMenu.h"
#include "PaddlePlayer.h"
#include "Ball.h"
#include "Score.h"
class MainGame : public TinyState
{
public:
void Initialize(sf::RenderWindow* window);
void Update(sf::RenderWindow* window);
void Render(sf::RenderWindow* window);
void Destroy(sf::RenderWindow* window);
private:
sf::Font* font;
PaddlePlayer* player1;
PaddlePlayer* player2;
Ball* ball;
Score* score1;
};
MainGame.cpp#include "MainMenu.h"
#include "MainGame.h"
void MainGame::Initialize(sf::RenderWindow* window)
{
this->font = new sf::Font();
this->font->loadFromFile("Graphics/font.otf");
//this->score1 = new Score(*font, 64U);
this->player1 = new PaddlePlayer(0);
this->player2 = new PaddlePlayer(1);
this->player2->setPosition(window->getSize().x - this->player2->getGlobalBounds().width,0);
this->ball = new Ball(player1, player2);
this->ball->setPosition(window->getSize().x / 2, window->getSize().y / 2);
}
void MainGame::Update(sf::RenderWindow* window)
{
this->ball->Update();
this->player1->Update();
this->player2->Update();
//this->score1->Update();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
{
coreState.SetState(new MainMenu());
}
}
void MainGame::Render(sf::RenderWindow* window)
{
//window->draw(*this->score1);
window->draw(*this->ball);
window->draw(*this->player1);
window->draw(*this->player2);
}
void MainGame::Destroy(sf::RenderWindow* window)
{
delete this->player1;
delete this->player2;
delete this->ball;
//delete this->score1;
delete this->font; //Problem occurring here
}