1
Graphics / SFML throws exception "Access violation reading location"
« on: March 28, 2016, 08:53:23 pm »
I got strange problem I mentioned in subject. Debbuger shows that exception throws in file button.cpp:
Debbuger throws me this exception whenever I use vector<Button> in MainMenu class:
#include "Button.h"
Button::Button(std::string ButtonTittle)
{
sf::FloatRect textBox;
this->buttonFont.loadFromFile("media/fonts/arial.ttf");
this->buttonTittle.setFont(this->buttonFont);
this->buttonTittle.setString(ButtonTittle);
textBox = this->buttonTittle.getLocalBounds();
this->buttonTittle.setOrigin((textBox.width / 2.0f), (textBox.height / 2.0f));
this->buttonTittle.setPosition(512.0f, 384.0f);
this->buttonShape.setSize(sf::Vector2f( (textBox.width + 4.0f), (textBox.height + 15.0f) ));
this->buttonShape.setOrigin((this->buttonShape.getSize()) / 2.0f);
this->buttonShape.setPosition(this->buttonTittle.getPosition());
this->buttonShape.setFillColor(sf::Color::Green);
}
Button::~Button()
{
}
void Button::Draw(sf::RenderWindow &gameWindow)
{
gameWindow.draw(this->buttonShape);
gameWindow.draw(this->buttonTittle);
}
void Button::MouseOverAction()
{
}
in line gameWindow.draw(this->buttonTittle);Button::Button(std::string ButtonTittle)
{
sf::FloatRect textBox;
this->buttonFont.loadFromFile("media/fonts/arial.ttf");
this->buttonTittle.setFont(this->buttonFont);
this->buttonTittle.setString(ButtonTittle);
textBox = this->buttonTittle.getLocalBounds();
this->buttonTittle.setOrigin((textBox.width / 2.0f), (textBox.height / 2.0f));
this->buttonTittle.setPosition(512.0f, 384.0f);
this->buttonShape.setSize(sf::Vector2f( (textBox.width + 4.0f), (textBox.height + 15.0f) ));
this->buttonShape.setOrigin((this->buttonShape.getSize()) / 2.0f);
this->buttonShape.setPosition(this->buttonTittle.getPosition());
this->buttonShape.setFillColor(sf::Color::Green);
}
Button::~Button()
{
}
void Button::Draw(sf::RenderWindow &gameWindow)
{
gameWindow.draw(this->buttonShape);
gameWindow.draw(this->buttonTittle);
}
void Button::MouseOverAction()
{
}
Debbuger throws me this exception whenever I use vector<Button> in MainMenu class:
#pragma once
#include <vector>
#include <string>
#include "VVisibleObject.h"
#include "Button.h"
class MainMenu : public VVisibleObject
{
public:
MainMenu(std::string ButtonTittle);
~MainMenu();
void ShowMenu();
static void AddButton(std::string ButtonTittle);
void operator+=(std::string ButtonTittle);
void Draw(sf::RenderWindow & gameWindow) override;
private:
std::vector<Button> Buttons;
//sf::Text GameTittle;
//Button button;
};
when using single object Button rather than vector of buttons game runs correctly, showing me button. I'm linking debug files (sfml-xxx-d.lib) correcty .. I suppose
#include <vector>
#include <string>
#include "VVisibleObject.h"
#include "Button.h"
class MainMenu : public VVisibleObject
{
public:
MainMenu(std::string ButtonTittle);
~MainMenu();
void ShowMenu();
static void AddButton(std::string ButtonTittle);
void operator+=(std::string ButtonTittle);
void Draw(sf::RenderWindow & gameWindow) override;
private:
std::vector<Button> Buttons;
//sf::Text GameTittle;
//Button button;
};