Jump to content

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

- - - - -

  • Please log in to reply
11 replies to this topic

#1
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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 ?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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 ?

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I've put img to initialization list; will call Image's constructor.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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);

}



#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Tried your code and it works. I had to make a call to
SDL_SetVideoMode()
first.

Quote

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
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
sandmaster

sandmaster

    Newbie

  • Members
  • PipPip
  • 20 posts
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

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
No worries, I'm glad you got it working. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users