Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Heap corruption while freeing memory  (Read 2151 times)

0 Members and 1 Guest are viewing this topic.

Juiz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Heap corruption while freeing memory
« on: June 23, 2016, 04:40:16 pm »
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
}

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Heap corruption while freeing memory
« Reply #1 on: June 23, 2016, 05:31:20 pm »
Are you sure that font loads correctly?
« Last Edit: June 23, 2016, 05:36:13 pm by Mr_Blame »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Heap corruption while freeing memory
« Reply #2 on: June 23, 2016, 11:11:45 pm »
I'm not really sure from the example you've given why it is you even need to heap allocate the font, when stack allocation will probably do. If you really do need to heap allocate your resources take a look at smart pointers.

Juiz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Heap corruption while freeing memory
« Reply #3 on: June 24, 2016, 05:19:45 pm »
Are you sure that font loads correctly?
Yes. I have another file for the menu and it load correctly. The problem happen only with this specific load.

I'm not really sure from the example you've given why it is you even need to heap allocate the font, when stack allocation will probably do. If you really do need to heap allocate your resources take a look at smart pointers.
The only thing that I am allocating is the sf::Font. The problem happens when a try to delete the font, and it is related with the loadFromFile() method, in some way that i don't know how to explain. I know it because when I comment this method, I don't get this error to delete.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Heap corruption while freeing memory
« Reply #4 on: June 24, 2016, 05:29:45 pm »
The point of fallahn's post was that you don't need any heap allocation at all. Work with stack variables and if you ever do need heap allocation use smart pointers and not manual new/delete.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/