7 Replies - 286 Views - Last Post: 17 June 2012 - 01:39 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

2D array of pointers problem

Posted 17 June 2012 - 12:34 PM

I am making a level class for my game and the program is crashing when the Level destructor gets called. Here is the code and I added some debug output and commented the line that I isolated the problem to. Can someone tell me if I'm deleting the array wrong or something?

Level.hpp
#ifndef LEVEL_HPP
#define LEVEL_HPP

#include "Globals.hpp"
#include "Tile.hpp"

class Level {
public:
	Level(sf::RenderWindow&);
	~Level();

	bool loadLevel(std::string);
	void render();
private:
	sf::RenderWindow* m_window;
	Tile*** m_tiles;
	int m_width, m_height;
};

#endif


Level.cpp
#include "Level.hpp"

Level::Level(sf::RenderWindow& window) {
	m_window = &window;
	m_width = (WIDTH / 32);
	m_height = (HEIGHT / 32);
	m_tiles = new Tile**[m_height];
	for (int i = 0; i < m_height; i++) {
		m_tiles[i] = new Tile*[m_width];
		for (int j = 0; j < m_width; j++) {
			m_tiles[i][j] = NULL;
		}
	}
}

Level::~Level() {
	cout << "Level destructor." << endl;
	for (int i = 0; i < m_height; i++) {
		for (int j = 0; j < m_width; j++) {
			cout << "Enterered" << endl;
			delete m_tiles[i][j];
			cout << "ended." << endl;
		}
		cout << "entered 1" << endl;
		delete [] m_tiles[i]; // this line is what isn't working -_-/>
		cout << "Ended." << endl;
	}
	cout << "entered 2" << endl;
	delete [] m_tiles;
	cout << "Level destructor ended." << endl;
}

bool Level::loadLevel(std::string filePath) {
	string currentLine;
	ifstream file(filePath);

	for (int i = 0; i < m_height; i++) {
		for (int j = 0; j < m_width; j++) {
			delete m_tiles[i][j];
			m_tiles[i][j] = NULL;
		}
	}

	if (file.is_open()) {
		for (int currentY = 0; currentY < m_height; currentY++) {
			getline(file, currentLine);
			for (int currentX = 0; currentX < currentLine.length(); currentX++) {
				m_tiles[currentY][currentX] = new Tile(currentX * 32, 
					currentY * 32, 
					(int)(currentLine[currentX] - '0'));
			}
		}
	} else {
		cout << "Could not find file: " << filePath << endl;
		return false;
	}
	return true;
}

void Level::render() {
	for (int i = 0; i < m_height; i++) {
		for (int j = 0; j < m_width; j++) {
			Tile* tile = m_tiles[i][j];
			if (tile->getType() != Types::EMPTY) {
				sf::Sprite* sprite = tile->getSprite();
				m_window->draw(*sprite);
			}
		}
	}
}


Thank you for any help, I know I've been asking a lot but I'm not asking for code, just what I'm doing wrong so I can fix it.

Is This A Good Question/Topic? 0
  • +

Replies To: 2D array of pointers problem

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: 2D array of pointers problem

Posted 17 June 2012 - 12:52 PM

First have you considered using std::vector instead of all the new/delete calls? Also your delete doesn't look correct, you have two calls to new so you need to calls to delete as well. But really I suggest you use vectors and let the vector take care of allocating and deleting the memory.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: 2D array of pointers problem

Posted 17 June 2012 - 12:56 PM

What's the error message provided when it crashes? Lack of NULL checks might indicate a double free at some point.
Was This Post Helpful? 1
  • +
  • -

#4 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: 2D array of pointers problem

Posted 17 June 2012 - 12:59 PM

I might change the code to use vectors, but for now I want to do it this way. And yes, if you look at the destructor, I loop through every row and then through every column in that row, once all the elements of that row are deleted, I delete the entire row, and then after all of that, I delete the the whole thing. I don't see what is wrong with the code -_-

I fixed the problem, I added in some checks for null pointers and that fixed it :) Thank you for the help!
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: 2D array of pointers problem

Posted 17 June 2012 - 01:02 PM

I think using vectors would be the best way to go about it like jimblumberg said. Your code works but at the moment it is a bit redundant and unnecessary.

Storing the level tiles in a vector and calling vector.clear() is much easier.
By the way, what kind of game are you making? :)

This post has been edited by v0rtex: 17 June 2012 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: 2D array of pointers problem

Posted 17 June 2012 - 01:07 PM

I can decide where I want to make a 2D side scrolling RPG, sort of like Zelda, or a side scrolling platformer similar to Mario. Which kind of game do you prefer? I think the first would be a much more challenging beast.
Was This Post Helpful? 0
  • +
  • -

#7 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: 2D array of pointers problem

Posted 17 June 2012 - 01:36 PM

To be honest, I would prefer a side scrolling platformer with a twist. Like perhaps make the levels larger and give the player a jet-pack or something. I just find platformers like Mario way too fun! (well I did)

In terms of being challenging, I think a zelda styled game would be more challenging due to the added RPG content but perhaps doing a platformer with a twist might be more interesting. Just my 2 cents :)
Was This Post Helpful? 0
  • +
  • -

#8 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: 2D array of pointers problem

Posted 17 June 2012 - 01:39 PM

That's what I was thinking too, what if it was a side scrolling platform game that wasn't linear progression?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1