Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ SDL problem- "Access violation reading location"

form

  • Please log in to reply
11 replies to this topic

#1 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 18 January 2012 - 06:26 AM

Hi

As the title says, I am getting an access violation reading location error when I run my program.

This happens when I am about to create an object of "Bombs" which holds an image(SDL_Surface).

I have the following code that there probably is something wrong with, since the error is surfacing.

Bombs.h:
Bombs( std::string str, int x, int y, float speed, int width, int height );

Bombs.cpp:
Bombs::Bombs( std::string str, int x, int y, float speed, int width, int height )
	: Sprite(str, x, y, 0, speed, width, height, 0.0f)
{
	img = new Image(str.c_str(), false);
	setAlive(true);
}

Image.cpp:
Image::Image( std::string filename, bool trans )
	{
		//Temporary stores the loaded image
		SDL_Surface* loadedImage = NULL;

		//Load the image from the filename
		loadedImage = IMG_Load( filename.c_str() );

		//Check if nothing went wrong with loading the image
		if( loadedImage != NULL )
		{
			//Set optimizedImage and make it optimized for the screen
			optimizedImage = SDL_DisplayFormat( loadedImage );

			//If the optimized image was optimizad just fine
			if( optimizedImage != NULL && trans == true )
			{
				//Locks the surface
				if( SDL_MUSTLOCK(optimizedImage) )
				{
					SDL_LockSurface( optimizedImage );
				}
								
				//Gets the first pixels value [Dangerous- only works with 32bit color]
				Uint32 transparent = *(Uint32*)optimizedImage->pixels;
				//Set it to transparent
				SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY | SDL_RLEACCEL,
					transparent );
				
				//Unlocks the surface
				if( SDL_MUSTLOCK(optimizedImage) )
				{
					SDL_UnlockSurface( optimizedImage );
				}

			//Free the old image
			SDL_FreeSurface( loadedImage );
			}		
		}	
		else
		{
			//Return an error if the file wasn't found or exist.			
			throw std::runtime_error(SDL_GetError());
		}

	}//End of constructor

Main program:
// Adds a bomb to the game window and set it into the vector
void addBomb( int x, int y, float speed )
{
	Bombs tmp("bomb.png", x, y, speed, 40, 40);
	bombs.push_back(tmp);
}


If there is any other code you want for figuring out the problem, just ask =)

Thanks
  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 18 January 2012 - 07:24 AM

What is main return code?

Image is a SDL_Surface wrapper but what is Sprite?

Since you're coding in C++ I'd recommend you check out SFML. It's cross platform and all packages come as one unlike SDL, where you have to set each one manually (image, mixer, net) and 2.0 is nearly here (official release, but you can build it for yourself), which includes lots of updates and new features.
  • 0

The roots of education are bitter, but the fruit is sweet.


#3 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 18 January 2012 - 07:36 AM

HI, well I have to use SDL, so the thing you suggested doesn't work =/

Sprite is a game object much like the character the player moves.
It also holds an image object as the image representing it.

The problem just starts when I add a bomb and otherwise it works

The main return code is 0 what i know of, don't really understand what you mean ?
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 18 January 2012 - 07:45 AM

Is bomb.png located in same directory?

Try this:
Bombs::Bombs( std::string str, int x, int y, float speed, int width, int height )
	: Sprite(str, x, y, 0, speed, width, height, 0.0f), [B]img(new Image(str.c_str(), false))[/B]
{
	//img = new Image(str.c_str(), false);
	setAlive(true);
}
In commented line, you're using assignment operator and if you haven't provided one, compiler will generate one for you, which might not behave as you want it.
  • 0

The roots of education are bitter, but the fruit is sweet.


#5 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 18 January 2012 - 09:01 AM

yes it is the same directory =)

Well wouldn't it not work at all (not have an image) when i comment the line as you did ?
  • 0

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 18 January 2012 - 03:54 PM

I've put img to initialization list; will call Image's constructor.
  • 0

The roots of education are bitter, but the fruit is sweet.


#7 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 18 January 2012 - 10:20 PM

ah, now I see it, but that didn't help at all with the problem =/ I still got the error message when I run it
  • 0

#8 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 January 2012 - 04:42 AM

About return code, I wasn't clear enough, sorry. I meant what code does your process return?

I can't really see what's causing the error, is it possible that you could paste/upload bombs and image files; maybe the bug is hidden somewhere else.
  • 0

The roots of education are bitter, but the fruit is sweet.


#9 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 19 January 2012 - 09:23 AM

Hi, yes here's the code of the full classes:

Image:

.h
#ifndef IMAGE_H
#define IMAGE_H

#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <iostream>

class Image
{
public:
	Image(); //Default Constructor
	Image( std::string filename, bool trans ); //Image constructor
	Image( const Image& img ); // Copy constructor
	SDL_Surface* get_Surface() const; //Gets the Image objects Surface
	const Image& operator= ( const Image& tmp ); //Accessoperator
	~Image( ); //Deconstructor
private:
	SDL_Surface* optimizedImage;

};

#endif

.cpp
#include "Image.h"

	Image::Image()
	{
		optimizedImage = NULL;
	}

	Image::Image( std::string filename, bool trans )
	{
		//Temporary stores the loaded image
		SDL_Surface* loadedImage = NULL;

		//Load the image from the filename
		loadedImage = IMG_Load( filename.c_str() );

		//Check if nothing went wrong with loading the image
		if( loadedImage != NULL )
		{
			//Set optimizedImage and make it optimized for the screen
			optimizedImage = SDL_DisplayFormat( loadedImage );

			//If the optimized image was optimizad just fine
			if( optimizedImage != NULL && trans == true )
			{
				//Locks the surface
				if( SDL_MUSTLOCK(optimizedImage) )
				{
					SDL_LockSurface( optimizedImage );
				}
								
				//Gets the first pixels value [Dangerous- only works with 32bit color]
				Uint32 transparent = *(Uint32*)optimizedImage->pixels;
				//Set it to transparent
				SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY | SDL_RLEACCEL,
					transparent );
				
				//Unlocks the surface
				if( SDL_MUSTLOCK(optimizedImage) )
				{
					SDL_UnlockSurface( optimizedImage );
				}

			//Free the old image
			SDL_FreeSurface( loadedImage );
			}		
		}	
		else
		{
			//Return an error if the file wasn't found or exist.			
			throw std::runtime_error(SDL_GetError());
		}

	}//End of constructor

	Image::Image( const Image& img)
		: optimizedImage(img.optimizedImage)
	{
		optimizedImage->refcount = img.optimizedImage->refcount;
	}

	SDL_Surface* Image::get_Surface() const
	{
		if( optimizedImage != NULL )
			return optimizedImage;
		else
			return 0;
	}

	const Image& Image::operator= ( const Image& tmp )
	{
		//Checks if the surface isnt itself
		if( this != &tmp )
		{
			//Set them to the same surface and add the refcount variable.
			optimizedImage = tmp.optimizedImage;
			// Deletes the old image
			SDL_FreeSurface(optimizedImage);
			// Adds one to the image objects refcount.
			tmp.optimizedImage->refcount++;
		}
		
		return *this;
	}

	Image::~Image( )
	{
		SDL_FreeSurface( optimizedImage );
	}



Bombs:
.h
#ifndef BOMBS_H
#define BOMBS_H

#include <string>
#include "Image.h"
#include "Sprite.h"

class Bombs : public Sprite
{
public:
	void Update();

	Bombs( std::string str, int x, int y, float speed, int width, int height );
private:
	float speed;
	int x, y;
	Image* img;

};


#endif
.cpp
#include "Bombs.h"

void Bombs::Update()
{
	y += speed;
}

Bombs::Bombs( std::string str, int x, int y, float speed, int width, int height )
	: Sprite(str, x, y, 0, speed, width, height, 0.0f), img(new Image(str.c_str(), false)
	)
{
	/*img = new Image(str.c_str(), false);*/
	setAlive(true);
}

  • 0

#10 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 January 2012 - 10:56 AM

Tried your code and it works. I had to make a call to
SDL_SetVideoMode()
first.

SDL_DisplayFormat makes a reference to the pixel format of the SDL's main screen, you make that with SDL_SetVideoMode

Taken from gamedev.net
  • 0

The roots of education are bitter, but the fruit is sweet.


#11 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 19 January 2012 - 11:05 AM

ah now i feel bad for my own ignorance:P

I had placed the bomb creation before initialization of SDL libraries :P and the display format :P

Thanks for the help =)

only if I had sent the whole main method at the start :P
  • 0

#12 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 January 2012 - 11:53 AM

No worries, I'm glad you got it working. :)
  • 0

The roots of education are bitter, but the fruit is sweet.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download