Wednesday 15 July 2015

c++ and xcode - calling objects function throws EXC_BAD_ACCESS -


This code works well in VS2010, but now I am trying to shut down my Mac in xcode 4.5 and this is giving me something bad. Access errors in run time In fact, I have a board class in which there is a 2d array of tiles, when I make the board, I can use the tile function, but later when I run my Draw function then I Gives poor access here. Here's a sample of my board class.

Board.h

  # Include class "Tile.h" class board {Private: // This is the 2D gameboard Tiles tile *** Game of the Gameboard ; Zero CreateBoard (const int size); Fill zero (); ... public: board (full size); Zero drawer (); ...}   

board.cpp

  board :: board (integer shape) {win = false; Lost = false; BoardSize = Size; Gameboard = new tile ** [size]; CreateBoard (size); } Zero Board :: CreateBoard (const int size) {... FillValues ​​()} Zero Board :: FillValues ​​() {for (int x = 1; x & lt; BoardSize + 1; x ++) {For (int y = 1; Y & lt; BoardSize + 1; y ++) {if (gameboard [x] [y] -> type () == "numberless") {Intel neighbors = counting ( X, Y); Gameboard [x] [y] - & gt; SetValue (neighbors); // it works}}} zero board :: drawboard () {for (int i = 0; i & lt; = boards + 1; i ++) {for (int j = 0; j & lt ; = BoardSystem + 1; J ++) {if (gameboard [i] [j] -> type ()! = "Border tile") {game board [i] [ja] - & gt; Draw (); // This does not work, I get an error while trying to use it - & gt; Type ()}}}} ...   

I call a function like this

  gi = new board (SCREEN_SIZE); GI- & gt; DrawBoard ();    

gamebird = new tile ** [size];

This just creates an array of tile ** . You do not have any real tile s or even tile * and later, when you type the array with the GameBoard [x] Attempting to access elements] [Y] - & gt; , you're killing undefined behavior.

If you have it, you will need to:

  GameBoard = new tile ** [size]; // Assign an array of tiles ** (int i = 0; i   

However, this is terrible. This is three very dynamic allocation, which you will have to remember in the end (and arrange it correctly).

A simple approach would be just 2D array tiles:

  Tile gameboard [CONSTEXPR_SIZE] [CONSTEXPR_SIZE];   

Or better yet, use the std :: array container:

  std :: arrays & lt; Std :: arrays & lt; Tile, CONSTEXPR_SIZE & gt;, CONSTEXPR_SIZE & gt; Board of Games;   

Here, the given size should be continuous expression. If you need to dynamically resize it, use a std :: vector instead.


In the comments below, you say that the size of your array is actually BoardSize + 1 . However, for your external and inner both code loops:

 for  (int i = 0; i   

It should be:

 for  (int i = 0; i & lt; boordes + 1; i ++)   

Also in the comment below, you say that type a char * . This means that you can not compare your string in this way:

  gameboard [i] [j] - gt; Type ()! = "BorderTile"   

This only indicates the comparison, since the left operand is a char * and the correct operand is const char * Is convertible to. It does not compare stars themselves, instead you want:

  gameboard [i] [j] - gt; Type ()! = Std :: string ("bordertile")   

This will be bound to be compromised using std :: string .

No comments:

Post a Comment