0

I was wondering if anyone of you could answer the question I am currently facing at.

After i compiled my code with g++ without any problems, i tried to execute the file. The window which i programmed with SFML opened for a very short time and closed right after that. My terminal gave me this error:

free(): double free deteted in tcache 2

The compiling command like this:

g++ main.cpp CObject.hpp CObject.cpp CFramework.hpp CFramework.cpp -o main.exe -lsfml-graphics -lsfml-window -lsfml-system

And the code like this:

CObject.hpp :

#include <SFML/Graphics.hpp>

#ifndef COBJECT_HPP
#define COBJECT_HPP

class CObject{
    public:
      CObject();
      ~CObject();

      void init(void);

      template <typename T, typename... Ts>
      void init(T vertex, Ts... vertex1);
      void setColor(sf::Color color);
      int m_vertexCount;
      sf::Vertex *m_vertexArray;
};

#endif

#include "CObject.tpp"

CObject.cpp :

#include "CObject.hpp"

CObject::CObject(void){
  m_vertexCount = 1;
  m_vertexArray = (sf::Vertex*) malloc(m_vertexCount * sizeof(sf::Vertex));

  if(m_vertexArray == NULL){exit(EXIT_FAILURE);}
}

CObject::~CObject(void){
  free(m_vertexArray);
}

void CObject::init(void){
  if(m_vertexArray == NULL){exit(EXIT_FAILURE);}
}

void CObject::setColor(sf::Color color){
  for(int i = 0; i < m_vertexCount; i++){
    m_vertexArray[i].color = color;
  }
}

COBject.tpp :

template <typename T, typename... Ts>
void CObject::init(T vertex, Ts... vertex1){
  if(m_vertexCount == 1){
    m_vertexArray[0] = (sf::Vertex) vertex;

    m_vertexCount += 1;
    m_vertexArray = (sf::Vertex*) realloc(m_vertexArray, m_vertexCount * sizeof(sf::Vertex));

    if(m_vertexArray == NULL){  exit(EXIT_FAILURE); }
  }else{
    m_vertexArray[m_vertexCount-1] = (sf::Vertex) vertex;

    m_vertexCount += 1;
    m_vertexArray = (sf::Vertex*) realloc(m_vertexArray, m_vertexCount * sizeof(sf::Vertex));

    if(m_vertexArray == NULL){  exit(EXIT_FAILURE); }
  }

  init(vertex1...);
}

CFramework.hpp :

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include "CObject.hpp"

#ifndef CFRAMEWORK_HPP
#define CFRAMEWORK_HPP

class CFramework{
public:
  CFramework();
  void init(int windowWidth, int windowHeight);
  void reset();
  void renderVertex(CObject object);
  void display();
  void close();

  sf::Event::EventType getEvent();
  sf::Event getKeyPressed();
  void handleWindowEvent(sf::Event::EventType windowEvent);

  static CFramework* getInstance();

  bool windowIsAlive;

protected:
  sf::RenderWindow window;
  sf::Event event;

private:
  static CFramework* Framework;
};

#endif

CFramework.cpp :

#include "CFramework.hpp"

CFramework::CFramework(){
  windowIsAlive = false;
}

CFramework* CFramework::Framework = NULL;

CFramework* CFramework::getInstance(){
  if(Framework == NULL){
    Framework = new CFramework;
  }

  return Framework;
}

void CFramework::init(int windowWidth, int windowHeight){
  window.create(sf::VideoMode(windowWidth, windowHeight), "Physics Simulation");

  windowIsAlive = true;
}

void CFramework::close(){
  window.close();
}

void CFramework::display(){
  window.display();
}

void CFramework::reset(){
  window.clear();
}

void CFramework::handleWindowEvent(sf::Event::EventType windowEvent){
  if(windowEvent == sf::Event::Closed){
    close();
    windowIsAlive = false;
  }
}

sf::Event::EventType CFramework::getEvent(){
  window.pollEvent(event);
  return event.type;
}

void CFramework::renderVertex(CObject object){
  window.draw(object.m_vertexArray, object.m_vertexCount, sf::TriangleStrip);
}

And lastly main.cpp :

#include "CFramework.hpp"

CFramework* Framework = CFramework::getInstance();

int main()
{
  CObject object;
  object.init(sf::Vertex(sf::Vector2f(10.f, 20.f), sf::Color::Red), sf::Vertex(sf::Vector2f(20.f, 20.f), sf::Color::Red), sf::Vertex(sf::Vector2f(20.f, 30.f), sf::Color::Red));

  Framework->init(600, 800);
  while(Framework->windowIsAlive == true){
    sf::Event::EventType eventType = Framework->getEvent();
    Framework->handleWindowEvent(eventType);
    Framework->reset();
    Framework->renderVertex(object);
    Framework->display();
  }
  return 0;
}

I think that my problem lays in the CObject files, but i yet wasn't able to find it.

Any help is appreciated.

  • 1
    Your object needs a copy constructor and an assignment operator. See the duplicate question for more information. Actually, it doesn't need it, and what it really needs is to get rid of `malloc` and `free`, and replace it with C++ containers that do all the allocations for you, and do it correctly. The best way to avoid bugs related to memory allocations is to simply not do it yourself, and let the C++ library do it for you, correctly. – Sam Varshavchik Oct 23 '20 at 12:09
  • Thanks for the quick answer! I looked arround on the internet and found the concept of containers in c++, espacially the concept of vectors. Those are resizable and allow me to input an unknown amount of sf::Vertex class instances from SFML. I wonder why i tried it myself without looking for available solutions which are already there. – Benjaniniooo Oct 23 '20 at 12:20
  • Although you might find some useful bits of information from random Google searches, you will not learn C++ effectively from some random Youtube video or a random web site. Any clown can upload a video, or publish a web site. The only way to thoroughly learn C++ [is from a quality textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which will go into detail not just about containers, but all other C++ fundamentals like iterators, algorithms and other topics. – Sam Varshavchik Oct 23 '20 at 12:43

0 Answers0