SFML - Access Violation Error?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1728 Views - Last Post: 10 January 2013 - 09:01 AM Rate Topic: -----

#1 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

SFML - Access Violation Error?

Posted 09 January 2013 - 07:44 AM

Hi D.I.C.

I was looking over RevTorA's tile engine tutorial and I decided to fool around with it. I changed some stuff as I'm using SFML 2.0, and not using XML to load the stage data (I like to write my own files), however that shouldn't have much to do with my error.

Anyways, I'm trying to see if I can draw the tiles to the screen using this code inside of Engine::Draw(), and I'm getting an access writing violation error in this function, which I'm not sure why or even how I can get such a thing. The vector map contains Tile objects and Draw( int x, int y, sf::RenderWindow* rw ) is not private.

void Level::Draw( sf::RenderWindow* rw )
{
	for( int x = 0; x != map.size(); ++x )
	{
		for( int y = 0; y != map.at( x ).size(); ++y )
		{
			map.at( x ).at( y )->Draw( ( x * 24 ), ( y * 30 ), rw );
		}
	}
}



VC++2010 opens Transformable.cpp and points to line 57 like that means a damn thing to me. There shouldn't be an access violation reading or writing as far as I know, unless I can't use map like that. SO....I tried this instead:

void Level::Draw( sf::RenderWindow* rw )
{
	for( int x = 0; x != map.size(); ++x )
	{
		for( int y = 0; y != map.at( x ).size(); ++y )
		{
			GetTile( x, y )->Draw( ( x * 24 ), ( y * 30 ), rw );
		}
	}
}



Still doesn't work.

The Draw function inside Tile is as follows:
void Tile::Draw( int x, int y, sf::RenderWindow* rw )
{
	baseSprite.setPosition( (float)x, (float)y );
	rw->draw( baseSprite );
}



As usual, thanks for reading. If there is a noob solution for this, please take it easy on me when you respond :D

Is This A Good Question/Topic? 0
  • +

Replies To: SFML - Access Violation Error?

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 09:04 AM

Can you show us how the map is defined and also the GetTile() function's definiton?
Was This Post Helpful? 0
  • +
  • -

#3 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 09:37 AM

Oh damn, sorry. Forgot to add GetTile()

// map is declared in Level.h
std::vector< std::vector<Tile*> > map;


Tile* Level::GetTile( int x, int y )
{
	return map[x][y];
}


Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 09:58 AM

How or where are the tiles added to the map? Since the map is a vector of a vector of pointers to Tiles, I would imagine that might be where your problem is. Do you know how to run the debugger? If so, it should be fairly easy to check where exactly the error is. Usually access violations have to do with either trying to dereference a pointer that is NULL or a pointer that points to some address that doesn't belong to the program.
Was This Post Helpful? 0
  • +
  • -

#5 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 10:22 AM

I'm still trying to figure out what the problem is. I thought I had it earlier, but it was something else. Thanks for reading.
Was This Post Helpful? 0
  • +
  • -

#6 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 10:49 AM

Wow man...I've stepped through this multiple times and from what I'm understanding, for some freaking reason the baseSprite in the Tile objects don't have a texture...but everything loads properly and all that, but somewhere along the way the texture just...vanishes?

Well this is probably going to make me give up programming cause evidently I'm no good at this. Thanks for reading and sorry for wasting time.

EDIT:

If anyone cares to read this:

If I declare an object pointer inside of a function and then pass the memory allocated to it somewhere else, like so:

void someFunc()
{
    Tile* _tile;
    _tile = new Tile( //blah blah );
    UseThisSomewhereElse( _tile );
}



The pointer goes out of scope after the function is done, but the memory is still held, right?

This post has been edited by GryphonClaws: 09 January 2013 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 10:52 AM

First off, you're not wasting time, second, if you give up that easy then maybe programming isn't for you. If you actually posted your program, instead of snippets, we probably could help you better. We're not mind-readers, we don't know what your other code does until we see it.
Was This Post Helpful? 2
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 10:58 AM

Im with vividexstance,

In fact you should really post the whole program warts and all...

Its probably going to be really hard for us to find the error and we have all been doing it
for a while.

But if you're the type who throws the toys out the pram at the first hurdle then by all means give up
and then give up at the next thing thats too hard and so on...

Life is about struggle we all struggle with life and code...

Some of us dream about our struggles with code...

and then it hits us and that Eureka moment man its like pure gold...

Snoopy
Was This Post Helpful? 1
  • +
  • -

#9 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 11:32 AM

Ignore the give up comment. Just feeling a little butthurt about my inability to figure this out, I'll put my pain in a dark corner now.

Here's the code I currently have. Each code tag block will contain the .h and .cpp for each file. And yes, before you comment, I know I have some other problems in there (like that assertion error I haven't figured out either). If I comment out
 _LevelOne.Draw( window ); 
in Engine::RenderFrame(), the access violation doesn't happen, so thats where I started my debug and I think I've narrowed it down to a problem with Tile::baseSprite dropping the texture for some reason, or ...something.

// Tile.h
#pragma once

#include <SFML/Graphics.hpp>

class Tile
{
public:
	enum TileType { NO_COLLIDE = 0, COLLIDE };

	sf::Sprite baseSprite;
	TileType _tileType;

public:
	Tile( sf::Texture& image, TileType type );
	~Tile();

	void Draw( int x, int y, sf::RenderWindow* rw );
	TileType GetTileType() { return _tileType; };
};


// Tile.cpp
#include "Tile.h"

Tile::Tile( sf::Texture& image, TileType type ) : _tileType( type )
{
	baseSprite.setTexture( image, true );
}

Tile::~Tile()
{
}

void Tile::Draw( int x, int y, sf::RenderWindow* rw )
{
	baseSprite.setPosition( (float)x, (float)y );
	rw->draw( baseSprite );
}



// ImageManager.h
#pragma once

#include <vector>
#include <SFML\Graphics.hpp>
#include <iostream>

class ImageManager
{
private:
	std::vector<sf::Texture> imageList;

public:
	ImageManager();
	~ImageManager();

	void AddImage( sf::Texture& image );
	sf::Texture& GetImage( int index );
	void UnloadImages();
};


// ImageManager.cpp
#include "ImageManager.h"

ImageManager::ImageManager()
{
}

ImageManager::~ImageManager()
{
}

void ImageManager::AddImage( sf::Texture& image )
{
	imageList.push_back( image );
}

sf::Texture& ImageManager::GetImage( int index )
{
	return imageList[ index ];
}

void ImageManager::UnloadImages()
{
// This isn't used right now, I was just testing something with it earlier
	for( int x = 0; x != imageList.size(); ++x )
	{
		std::cout << "Currently Deleting Image number " << x << "!" << std::endl;
		delete &imageList.at( x );
	}
	
	imageList.clear();
}



// Level.h
#pragma once

#include <vector>
#include "Tile.h"
#include <string>
#include <fstream>
#include "ImageManager.h"
#include <iostream>

class Level
{
private:
	//A 2D array of Tile pointers
	std::vector< std::vector<Tile*> > map;

	//Width and height of level (in tiles)
	int _w;
	int _h;

	// Bool variable for UnloadLevel()
	bool _isLoaded;

	void SetDimensions( int w, int h );
	
public:
	Level( int w, int h );
	Level();
	~Level();

	void AddTile( int x, int y, Tile* tile );
	Tile* GetTile( int x, int y );
	void Draw( sf::RenderWindow* rw );

	bool LoadLevel( std::string filename, ImageManager& imgMgr );
	void UnloadLevel();

	int GetWidth() { return _w; };
	int GetHeight() { return _h; };
};

// Level.cpp
#include "Level.h"

Level::Level( int w, int h ) : _isLoaded( false )
{
	SetDimensions( w, h );
	_w = w;
	_h = h;
}

Level::Level() : _isLoaded( false ), _w( 0.0f ), _h( 0.0f )
{
}

Level::~Level()
{

}

void Level::SetDimensions( int w, int h )
{
	//w rows
	map.resize( w );

	//Each row has h columns of null Tile pointers
	for( int i = 0; i < w; i++ )
	{
		map.at( i ).resize( h, 0 );
	}
}

void Level::AddTile( int x, int y, Tile* tile )
{
	map[x][y] = tile;
}

Tile* Level::GetTile( int x, int y )
{
	return map[x][y];
}

bool Level::LoadLevel( std::string filename, ImageManager& imgMgr )
{
	std::ifstream fin;
	int code;
	std::string word;
	fin.open( filename.c_str(), std::ios::beg );

	if( !fin.is_open() )
	{
		// File couldn't be opened.
		std::cout << "Map file " << filename << " could not be loaded!" << std::endl
			<< "Make sure the file is in the mapdata folder! Program will now end!" << std::endl;
		system( "PAUSE" );
		_isLoaded = false;
		return false;
	}

	while( !fin.eof() )
	{
		fin >> word;

		if( word == "Width:" )
		{
			fin >> code;
			_w = code;
		}

		if( word == "Height:" )
		{
			fin >> code;
			_h = code;
		}

		if( word == "TileData:" )
		{
			SetDimensions( _w, _h );

			for( int y = 1 ; y != _h; ++y )
			{
				for( int x = 1; x != _w; ++x )
				{
					fin >> code;	// Numerical code denoting which image to use for the tile
					fin >> word;	// Word denoting collision type
					Tile *temp;

					if( word == "C" )
					{
						temp = new Tile( imgMgr.GetImage( code ), Tile::TileType::COLLIDE );
						AddTile( x, y, temp );
					}

					if( word == "NC" )
					{
						temp = new Tile( imgMgr.GetImage( code ), Tile::TileType::NO_COLLIDE );
						AddTile( x, y, temp );
					}
				}
			}
		}
	}

	_isLoaded = true;
	return true;
}

void Level::Draw( sf::RenderWindow* rw )
{
	int x_, y_;

	for( int x = 0; x != map.size(); ++x )
	{
		for( int y = 0; y != map.at( x ).size(); ++y )
		{
			GetTile( x, y )->Draw( ( x * 24 ), ( y * 30 ), rw );
		}
	}
}

void Level::UnloadLevel()
{
	if( _isLoaded )
	{
		for( int x = 0; x != map.size(); ++x )
		{
			for( int y = 0; y != map.at( x ).size(); ++y )
			{
				std::cout << "Currently deleting Level map[ " << x << " ][ " << y << " ]!" << std::endl;
				delete map.at( x ).at( y );
			}
		}

		_isLoaded = false;
		map.clear();
	}
}



// Engine.h
#pragma once

#include <SFML\Graphics.hpp>
#include "ImageManager.h"
#include "Level.h"

class Engine
{
private:
	//SFML Render Window
	sf::RenderWindow* window;
	ImageManager _ImageManager;
	Level _LevelOne;

	//Initializes the engine
	bool Init();				
	//Main Game Loop
	void MainLoop();			
	//Renders one frame
	void RenderFrame();
	//Processes user input
	void ProcessInput();
	//Updates all Engine internals
	void Update();				
	// Cleans up all open memory
	void CleanUp();

public:
	Engine();
	~Engine();

	void Go();					//Starts the engine
};

// Engine.cpp

#include "Engine.h"
#include <SFML\Graphics.hpp>

Engine::Engine()
{
}

Engine::~Engine()
{
}

bool Engine::Init()
{
	window = new sf::RenderWindow( sf::VideoMode( 800, 600 ), "Test Window" );

	if(!window)
		return false;

	// Load Images
	sf::Texture _tex, tex2;
	if( !_tex.loadFromFile( "./images/tile_skyBlue.png" ) )
	{
		return false;
	}

	_ImageManager.AddImage( _tex );

	if( !tex2.loadFromFile( "./images/tile_darkBlueBlock.png" ) )
	{
		return false;
	}

	_ImageManager.AddImage( tex2 );

	// Load Level File
	if( !_LevelOne.LoadLevel( "./mapdata/testStage.txt", _ImageManager ) )
	{
		return false;
	}

	return true;
}

void Engine::RenderFrame()
{
	window->clear();

	_LevelOne.Draw( window );

	window->display();
}

void Engine::ProcessInput()
{
	sf::Event evt;
	//Loop through all window events
	while(window->pollEvent(evt))
	{
		switch( evt.type )
		{
		case sf::Event::Closed:
			window->close();
			break;

		case sf::Event::KeyPressed:
			if( sf::Keyboard::Escape )
				window->close();
			break;

		default:
			break;
		}
	}
}

void Engine::Update()
{

}

void Engine::MainLoop()
{
	//Loop until our window is closed
	while( window->isOpen() )
	{
		ProcessInput();
		Update();
		RenderFrame();
	}

	CleanUp();
}

void Engine::Go()
{
	if(!Init())
		throw "Could not initialize Engine";

	MainLoop();
}

void Engine::CleanUp()
{
	_LevelOne.UnloadLevel();
	//_ImageManager.UnloadImages();
	delete window;
}




And finally testStage.txt

Width: 2
Height: 2
TileData:
0 C 0 C
1 NC 1 NC


Was This Post Helpful? 0
  • +
  • -

#10 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 12:34 PM

I just installed SFML 2.0 and I was able to compile all those files after making a couple of changes, could you post the file with the main() function in it?

A couple of the changes were simple, you attempt to open a file on line 47 in Level.cpp with a second argument of std::ios_base::beg. This is incorrect, the second argument is whether the file should be opened for reading or writing, or both. Also, I was getting two errors where you create the tiles in Level.cpp on lines 89 and 95. So I moved the enum in the class Tile outside of it and it resolved the issue. I'm not sure why it wouldn't the other way you had it.

Have a look at this page for how you change the "get" position in a file stream to the beginning of the file: http://www.cplusplus.../istream/seekg/

This post has been edited by vividexstance: 09 January 2013 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#11 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 01:59 PM

Hmm...I've always opened files like that and never had any problems. Thanks for the link.

Here's the winmain I'm using. I thought I posted that as well, so I think I'm actually going to sleep after posting this. I've been up since 12a.m. and it's showing big time.

#include <Windows.h>
#include "Engine.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// This is code I found online to activate the console. Just trying that out as well
	AllocConsole();
	freopen("conin$","r",stdin);
	freopen("conout$","w",stdout);
	freopen("conout$","w",stderr);
	printf("Debugging Window:\n");
	
	Engine engine;

	try
	{
		engine.Go();
	}
	catch(char* e)
	{
		MessageBoxA(NULL, e, "Exception Occured", MB_OK | MB_IConerror);
	}
}




I'm not sure why, but sometimes when I use enums that same thing happens. They won't work until I move them outside of the class I need them for.
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 04:37 PM

I'm still looking but when I debug your program, the seg fault is coming from:

Quote

Program received signal SIGSEGV, Segmentation fault.
0x0015ce92 in sf::Texture::getSize() const () from /usr/lib/libsfml-graphics.so.2

Which is apparently called when execution reaches line 38 in Engine.cpp.

*EDIT*: I just debugged it a little more and now I'm getting the seg fault in the Level::LoadLevel() function.

I'll keep checking it, but for now I'm pretty sure the seg fault is coming from the ImageManager::GetImage() member function. This is because you attempt to access an element of the imageList vector using the subscript operator, but the vector is empty. So you should put a check in there to see if it's empty before you try to use it.


I had to comment out the part in Engine.cpp where you add the images and thats why I had the problem above. Could you post those two images so I can really test it?

This post has been edited by vividexstance: 09 January 2013 - 05:40 PM

Was This Post Helpful? 1
  • +
  • -

#13 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 05:55 PM

http://www.freeimagehosting.net/vq4g5 <- tile_darkBlueBlock.png
http://www.freeimagehosting.net/foecn <- tile_skyBlue.png

Thanks for going so far vividexstance. I'm beginning to think it's the .dll but I don't think anyone else is having problems with it, so it must be my code. Geez it's annoying.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: SFML - Access Violation Error?

Posted 09 January 2013 - 09:27 PM

When you load the level you start adding tiles at x and y equal to 1, is the ok?

120	            for( int y = 1 ; y != _h; ++y )
121	            {
122	                for( int x = 1; x != _w; ++x )


Was This Post Helpful? 1
  • +
  • -

#15 GryphonClaws  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-December 12

Re: SFML - Access Violation Error?

Posted 10 January 2013 - 01:11 AM

.....


No. This is not ok. This is why I should really sleep when I need to. Thanks for the help guys.

edit:
Yeah, I remember now, I changed x and y from 0 to 1 by using the 'Undo' command too many times, but I never checked it. Why didn't I get an out of range error instead of a access violation?

That was the out of range error I was getting as well. I think I will see this in the bad code area :/

This post has been edited by GryphonClaws: 10 January 2013 - 01:20 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2