I have a class Block
which calls SDL_FreeSurface(surface)
in the destructor. In the main()
when I create an instance of Block, the object functions properly but when I used it in another class Control
,which has vector<Block> block_vector
as a data member, the program crashes when I add an instance of Block
into the block_vector
. I narrowed down the problem which is the destructor of the Block
when calling SDL_FreeSurface(surface)
. Does adding objects to the vector have anything to do with is? What is the problem?
class Block{
public:
Block(int x, int y);
~Block();
void Load_Image(MediaFunctions &M_Functions);
void SetPosition(int x, int y);
void BlitBlock(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool DetectionNames(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool DetectionHours(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);
bool return_error();
private:
SDL_Surface *block_surface_names;
SDL_Surface *block_surface_hours;
SDL_Surface *block_names_detected;
SDL_Surface *block_hours_detected;
SDL_Rect block_rect_names;
SDL_Rect block_rect_hours;
bool error;
};
//the problem
Block::~Block(){
SDL_FreeSurface(block_surface_hours);
SDL_FreeSurface(block_surface_names);
SDL_FreeSurface(block_hours_detected);
SDL_FreeSurface(block_names_detected);
}
//when doing this
void Control::HandleEvents(SDL_Event &event, MediaFunctions &M_Functions){
if(event.type == SDL_KEYDOWN){
if( event.key.keysym.sym == SDLK_a ){
//append a block instance
BlockVector.push_back(Block (Block(100,100)));
BlockVector.at(block_index).Load_Image(M_Functions);
block_index++;
}
}
}