Access violation reading location

I'm trying to make a menu object in SFML. So far all I've added is the ability to add buttons with text inside of them. I store the buttons in a vector and then loop through that vector to draw the buttons.

The error stems from line 24 of the 2nd piece of code but I've included the majority of relevant code to help get an idea of what is going on.

Here's what I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//MenuTest.h
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>

class Menu1
{
	class Button1
	{
		sf::RectangleShape body;
		sf::Text text;
		void create(float x, float y);//sets button postion
		void setText();//ensures text is centered inside the button
	public:
		void setPosition(float x, float y);
		void drawButton(sf::RenderWindow &window);

		Button1(float w, float h, float x, float y, sf::Font f, std::string s="Button") 
                    : body(sf::Vector2f(w,h)), text(s,f) {create(x,y);}
		
	};

	std::vector<Button1> MenuButtons;
public:
	void draw(sf::RenderWindow &window);
	void addButton(float w, float h, float x, float y, sf::Font f, std::string s="Button");
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "MenuTest.h"

//Button1---
void Menu1::Button1::create(float x, float y)
{
	setPosition(x,y);
}

void Menu1::Button1::setText()
{
	/* Bunch of stuff to ensure text is inside of button
            Removing this and setting the Text object to some
            random spot does not effect the problem */
		
}
void Menu1::Button1::setPosition(float x, float y)
{
	body.setPosition(x,y);
	setText();
}
void Menu1::Button1::drawButton(sf::RenderWindow &window)
{
	window.draw(body);
	window.draw(text);//<--- Error comes from here
}

//Menu1----
void Menu1::addButton(float w, float h, float x, float y, sf::Font f, std::string s)
{
	MenuButtons.push_back(Button1(w,h,x,y,f,s));
}
void Menu1::draw(sf::RenderWindow &window)
{
	for(int i=0;i<MenuButtons.size();i++)
		MenuButtons[i].drawButton(window);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "MenuTest.h"
#include <iostream>
void createMenu(sf::RenderWindow &window)
{
	Menu1 main;
	sf::Font font;
	if(!font.loadFromFile("Terminal.ttf"))
		std::cout<<"Font error\n";
	main.addButton(100,50,20,20,font);
	main.draw(window);
}


int main()
{
	sf::RenderWindow window(sf::VideoMode(1000,800),"Window");
	window.clear();
	createMenu(window);
	
	window.display();
	char a;
	std::cin>>a;
	window.close();
    return 0;
}


This causes an error at the line window.draw(text); in Menu::Button::drawButton(). With the error "Access violation reading location (some memory address)". I'm going through now to try and figure out what the problem is.
Everything up to trying to draw the text works fine.

Sorry for the block of text. Did my best to cut out anything not relevant to the problem.

EDIT: trimmed down some stuff that wasn't relevant
Last edited on
In the event that anyone else ever reads this the solution can be found at

http://en.sfml-dev.org/forums/index.php?topic=15241.0
Topic archived. No new replies allowed.