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.