I do get a Valgrind error when running it.
At this statement: " imageSurfaces[newIndex] = *optimizedImage;"
It says that "address 0x0 is not stack'd malloc'd or recently free'd".
This should mean that I'm trying to dereference a null pointer, right? It looks like it could theoretically happen but I've ran the program, printing out the value of optimizedImage and imageSurfaces (the address, not the value) the line before the incriminating call, and at no point did they display 0x0.
ResourceManager.h
class ResourceManager
{
private:
SDL_Surface * imageSurfaces;
int numImageSurfaces;
int sizeOfImageSurfaceArray;
public:
void apply_surface( int x, int y, int resourceId, SDL_Surface* destination );
int load_image( std::string filename );
void displayList();
ResourceManager() { imageSurfaces = new SDL_Surface[1]; sizeOfImageSurfaceArray = 1; }
~ResourceManager() {
for ( int i=0; i< numImageSurfaces; i++ ) { SDL_FreeSurface( &(imageSurfaces) ); } delete imageSurfaces;
}
};
ResourceManager.cpp
#include "resourcemanager.h"
int ResourceManager::load_image( std::string filename ) {
SDL_Surface* loadedImage = NULL; //Temporary storage for the image that's loaded
SDL_Surface* optimizedImage = NULL; //The optimized image that will be used
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL ) {
//Create an optimized image
optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); //Free the old image
SDL_FreeSurface( loadedImage );
}
int newIndex = numImageSurfaces;
// manually manage the resource array, because references to vector contents apparently do not persist through expansions
if( numImageSurfaces >= sizeOfImageSurfaceArray)
{
sizeOfImageSurfaceArray *= 2;
SDL_Surface * newArray = new SDL_Surface[sizeOfImageSurfaceArray];
for( int i = 0; i<newIndex; i++)
{
newArray = imageSurfaces;
}
delete imageSurfaces;
imageSurfaces = newArray;
}
numImageSurfaces++;
imageSurfaces[newIndex] = *optimizedImage;
return newIndex;
}
// Applies the surface referenced by the resourceId to the destination surface
void ResourceManager::apply_surface( int x, int y, int resourceId, SDL_Surface* destination ) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( &(imageSurfaces[resourceId]), NULL, destination, &offset );
}