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