access violation vector

Hi,

I am trying to manipulate a vector of vector<bool>. For some reason, the vector's reference seems to get lost at some point. When I try to access the elements, I get an access violation error.

The code I am printing is a bit messy because I am working on it and exprimenting somewhat. Anyways, here is the header file with the vector<vector<bool>> as a private member of the class :
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once

#include <iostream>
#include <stdio.h>
#include <sfml/system.hpp>
#include <sfml/graphics.hpp>
#include <sfml/window.hpp>
#include <vector>
#include "line.h"
#include "bar.h"

using namespace sf;
using namespace std;

class ball
{
public:
	ball(Texture&);
	ball(Vector2f position, Vector2f speed, Texture& texture);
	ball(RenderWindow&, Vector2f position, Vector2f speed, Texture& texture);
	ball(RenderWindow&, Vector2f position, Vector2f speed, Texture& texture, Vector2f scale);
	ball(Vector2f position, Vector2f speed, Texture& texture, Color);
	~ball();
	
	void buildMask();
	void setPosition(Vector2f);
	Vector2f getPosition();

	void collisionManager(line aLine);
	void collisionManager(ball&, ball&);
	void collisionManager(bar&);

	bool isOutOfBound();
	void collideWithLine(Vertex&, Vertex&);
	void collideWithLineTip(Vertex&, Vertex&);
	bool canHitLine(Vertex&, Vertex&);
	bool canCollideWithBar(bar&);

	vector<Vertex> getPolygonPoints(bar&);

	float distanceBetweenLineAndSprite(line aLine);
	float distanceBetweenPointAndSprite(Vertex aPoint);
	float distanceBetweenSpriteAndSprite(ball&, ball&);
	float getTrueAngleLine(Vertex&, Vertex&);

	float getTrueAngleSpeed();
	float getReflectedAngle(float, float);
	float calcNormalizedAngle(float);
	float getRadius();
	Sprite& getSprite();

	Color& getColor();
	Vector2f& getScale();

	void gravityEffect();
	void move();
	void update();

	

protected:
	RenderWindow& myWin;

	Texture& myTexture;
	Sprite mySprite;

	Vector2f myPosition;
	Vector2f mySize;
	Vector2f myScale;
	Vector2f mySpeed;
	float myMass;
	float kinEnergy;
	
	vector<vector<bool>> myMask;

	bool inCollision;

	Color myColor;

	Clock myClock;

	bool hadCollision;

	CircleShape myCircle;

	float speedCorrection;

private:

};


Here is the constructor where I call the function (buildMask()) to give values to my vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ball::ball(RenderWindow& aWin, Vector2f aPosition, Vector2f aSpeed, Texture& aTexture, Vector2f aScale)
	:myWin(aWin), myTexture(aTexture), myPosition(aPosition), mySpeed(aSpeed), myScale(aScale)
{
	inCollision = false;
	//myColor = Color(255,255,255,255);
	int RGB = randBetween_0_1() * 255;
	myColor = Color(RGB,RGB,255,255);

	mySprite.setColor(myColor);
	mySprite.setPosition(myPosition);
	myTexture = aTexture;
	mySprite.setTexture(myTexture);
	mySprite.setOrigin(mySprite.getLocalBounds().width/2, mySprite.getLocalBounds().height/2);

	buildMask();


}





Here is the function buildMask() :
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
37
38
void ball::buildMask()
{
	Image anImage;
	RenderTexture Rtex;
	Texture aTexture;




	Sprite tempprite(mySprite);
	tempprite.setPosition(getSprite().getOrigin().x,getSprite().getOrigin().y);
	Rtex.create(tempprite.getGlobalBounds().width, tempprite.getGlobalBounds().height);
	Rtex.draw(tempprite);
	Rtex.display();
	aTexture = Rtex.getTexture();
	
	anImage = aTexture.copyToImage();
	vector<vector<bool>> myMask;
	

	for(int i = 0; i < anImage.getSize().x; ++i){
		vector<bool> tempMask;
		for(int j = 0; j < anImage.getSize().y; ++j){
			if(anImage.getPixel(i,j).a > 200){
					cout << "1";
				tempMask.push_back(true);
			}else{
				tempMask.push_back(false);
				cout << "0";
			}
		}
		cout << endl;
		myMask.push_back(tempMask);
	}

	cout << myMask[0].size() << "    " << myMask.size() << endl;
}


Within the scope of this previous function, I can cout the vector's size, being 10 10. Later on, I try to acess the content in another of its member function but this time, it crashes.
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
37
38
39
40
41

void ball::collisionManager(bar& aBar)
{
	
	Image anImage;
	RenderTexture Rtex;
	Texture aTexture;




	Sprite aSprite2(aBar);
	Texture wtvTexture;

	wtvTexture.loadFromFile("Ressources/bar_left.png");
	aSprite2.setPosition(0,0);
	Rtex.create(aSprite2.getGlobalBounds().width, aSprite2.getGlobalBounds().height);
	Rtex.draw(aSprite2);
	Rtex.display();

	aTexture = Rtex.getTexture();
	
	anImage = aTexture.copyToImage();
	vector<vector<bool>> barMask;
	

	for(int i = 0; i < anImage.getSize().x; ++i){
		vector<bool> tempMask;
		for(int j = 0; j < anImage.getSize().y; ++j){
			if(anImage.getPixel(i,j).a > 0){

				tempMask.push_back(true);
			}else{
				tempMask.push_back(false);

			}
		}
		barMask.push_back(tempMask);
	}
	cout << barMask[0].size() << "    " << barMask.size() << endl;


This next line produces an acess violation error
1
2
3
	cout << myMask[0].size() << "    " << myMask.size() << endl;



The rest of the code below dont matter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	float colBottom, colTop, colLeft, colRight;
	colBottom = max(getSprite().getGlobalBounds().top, aBar.getGlobalBounds().top);
	colTop = min(getSprite().getGlobalBounds().top + getSprite().getGlobalBounds().height, aBar.getGlobalBounds().top + aBar.getGlobalBounds().height);
	colLeft = max(getSprite().getGlobalBounds().left, aBar.getGlobalBounds().left);
	colRight = min(getSprite().getGlobalBounds().left + getSprite().getGlobalBounds().width, aBar.getGlobalBounds().left + aBar.getGlobalBounds().width);
	cout << "Colbottom = " << colBottom << endl << "ColTop = " << colTop << endl << "Colleft = " << colLeft << endl << "Colright = " << colRight <<endl;
	cout << "ball position :  x = " << mySprite.getGlobalBounds().left << "   y = " << mySprite.getGlobalBounds().top;
	cout << "test " << (int)aBar.getGlobalBounds().left << endl;
	for(int i = colBottom; i < colTop; ++i){
		for(int j = colLeft; j < colRight; ++j){
			cout << j - mySprite.getGlobalBounds().left << endl << i - mySprite.getGlobalBounds().top << endl;
			if(barMask[j - mySprite.getGlobalBounds().left][i - mySprite.getGlobalBounds().top]){// && myMask[j - mySprite.getGlobalBounds().left][i - mySprite.getGlobalBounds().top]){
				cout << "There is collision" << endl;
			}
		}
	}

}


Anybody can tell me why I get the access violation error? I believe I made a mistake somewhere but I can't find it.
Last edited on
In ball::buildMask on line 18, you define a variable that has the same name as the data member myMask. Any further unqualified uses of myMask in the function refer to the local variable, and do not affect the member variable in any way, so this->myMask is empty when buildMask returns.
Oups....... thanks for pointing it out!
Topic archived. No new replies allowed.