Access-violation reading location

So I have been trying to eradicate this error for a few hours now but I was unable to. I am making a 2D game where I am reading different maps for different screens.

This is where I get the error. I call this in my main. Here, curr_map_ is a string and mapper is an object of Map.
 
curr_map_ = mapper.GetMapLabels();


Here is my Map 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <mylibrary/map.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using mylibrary::Direction;
using mylibrary::Location;

int prev_row = 0;
int prev_col = 0;

namespace mylibrary {

Map::Map() = default;

Map::Map(std::vector<std::vector<char>> game_screen) {
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      this->coordinates_[i][j] = game_screen[i][j];
    }
  }
}

void Map::ReadImageLabels() {
  std::string map_label_file = "C:/Users/monty/CLionProjects/cinder_0.9.2_vc2015/projects/Break/assets/background.txt";
  std::ifstream file(map_label_file);

  while(!file.eof()) {
    std::string map_label;
    std::getline(file, map_label);
    map_labels_.push_back(map_label);
  }
}

void Map::ReadGameScreens() {
  int map_line_count = 0;
  std::string maps_file = "C:/Users/monty/CLionProjects/cinder_0.9.2_vc2015/projects/Break/assets/maze.txt";
  std::ifstream file(maps_file);
  while (!file.eof()) {
    std::string map_line;
    std::getline(file, map_line);
    if (!map_line.empty()) {
      SetupMap(map_line);
      map_line_count++;


      if (map_line_count == 16) {
        Map game_screen = Map(map_);
        game_maps_.push_back(game_screen);

        map_line_count = 0;
        map_.clear();
      }
    }
  }
}

void Map::SetupMap(std::string map_line) {
  std::vector<char> map_line_char;
  map_line_char.reserve(16);
  for (int i = 0; i < 16; i++) {
    map_line_char.push_back(map_line.at(i));
  }
  map_.push_back(map_line_char);
}

std::string Map::GetMapLabels() {
  for (int i = 0; i < map_labels_.size(); i++) {
    if (i == screen_num_) {
      return map_labels_[i];
    }
  }
}

std::vector<Map> Map::GetScreen() {
  return game_maps_;
}

bool Map::IsScreenChange() {
  return is_screen_change_;
}

int Map::GetNewScreenNum() {
  return screen_num_;
}

Location Map::GetPlayerNewLoc(const Map& curr_map, Engine engine) {
  Location location = engine.GetPrisoner().GetLoc();
  int curr_row = location.Col();
  int curr_col = location.Row();

  for (int j = 0; j < entry_points_.size(); j++) {
    if (curr_map.coordinates_[curr_row][curr_col] == entry_points_.at(j)) {
      screen_num_ = GetTransitionScreenNum(GetCurrScreenNum(curr_map),
                                           entry_points_.at(j));
      is_screen_change_ = true;
      if (engine.GetDirection() == Direction::kUp) {
        return {curr_col, 11};
      } else if (engine.GetDirection() == Direction::kDown) {
        return {curr_col, 1};
      } else if (engine.GetDirection() == Direction::kLeft) {
        return {14, curr_row};
      } else if (engine.GetDirection() == Direction::kRight) {
        return {1, curr_row};
      }
    }
  }

}

int Map::GetCurrScreenNum(const Map& curr_map) {
  int count = 0;
  for (int i = 0; i < game_maps_.size(); i++) {
    for (int j = 0; j < 16; j++) {
      for (int k = 0; k < 16; k++) {
        if (game_maps_[i].coordinates_[j][k] == curr_map.coordinates_[j][k]) {
          count++;
        } else {
          count = 0;
          goto outerloop;
        }
        if (count == 256) {
          return i;
        }
      }
    }
    outerloop:;
  }
}

int Map::GetTransitionScreenNum(int num, char entry) {
  for (int i = 0; i < game_maps_.size(); i++) {
    if (i != num) {
      for (int j = 0; j < 16; j++) {
        for (int k = 0; k < 16; k++) {
          if (game_maps_[i].coordinates_[j][k] == entry) {
            return i;
          }
        }
      }
    }
  }
}

}  // namespace mylibrary


My text files are -

maze.txt

1111111111111111
0000000100100101
1011110100000101
1010010001110001
1010110001010111
1011010100000001
1010010111101101
1000010100100101
1011011100101100
1001000000110001
1001001110001111
1011101000000001
1010001000110111
1011011011100111
10000100101000d1
1111111111111111

aaaaaaaaaaaaaaaa
bbbbbbbabbabbaba
abaaaababbbbbaba
ababbabbbaaabbba
ababaabbbababaaa
abaabababbbbbbba
ababbabaaaabaaba
abbbbababbabbaba
abaabaaabbabaaab
abbabbbbbbaabbba
abbabbaaabbbaaaa
abaaababbbbbbbba
ababbbabbbaabaaa
abaabaabaaabbadd
ddbbbabbababbbdd
aaaaaaaaaaaaaaaa

background.txt

maze1.png
maze2.png


Any help will be appreciated.
The line that you say generates the errors doesn't even appear in the code that you posted, which makes it hard to diagnose.

From your description, the line of code appears to be okay. That probably means that the actual error is before that line, probably on the previous line. It's just that the compiler didn't realize there was a problem until it got to the line you posted.
try adding a return "" to GetMapLabels so it returns something valid even if the normal return did not work?
I tried that, it didn't help.

It gives me the same error.
then the error is not where you think it is. there are only 2 things that can go wrong with the function you indicated. The first is if you managed to go out of bounds in your loop. It can't do that, as your loop is worked off size() which is assured to work (unless you have some weird offbeat compiler and found a bug in it). The second is it skips the returns and the result of the call is undefined, which could cause an access blow up but you said fixing that error did not help. That indicates the error is actually somewhere else. The only way I know to find the issue when the errors are unhelpful is the binary search, code style. go into main and start commenting out lines in main until it runs, then put them back until it crashes, then you know what to trace.
Please post the declaration of class Map.

Ideally, please post a program that compiles and demonstrates the problem.
Topic archived. No new replies allowed.