我有一個向量,添加包含SDL_Surface
指針作爲數據成員的對象,這意味着我必須使用複製構造函數來實現指針的深度複製。該對象釋放析構函數中的曲面(指針),這是問題發生的地方。目前對象被添加到向量中(通過按下按鈕)程序崩潰,但是當我從析構函數(內存泄漏)中拿走SDL_FreeSurface(surface)
時,程序不會崩潰,當我將該對象添加到向量時。如何正確地將對象添加到矢量中?有些人可能會認爲問題在於析構函數試圖刪除懸掛指針,但是在向量中創建對象時發生錯誤。C++添加指針到對象的對象
class Block{
public:
Block(int x, int y, MediaFunctions &M_Functions);
Block(const Block& source);
~Block();
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;
};
////////////////////
Block::Block(int x, int y, MediaFunctions &M_Functions){
block_surface_names = M_Functions.LoadOptimizedImage("block_names.png");
block_surface_hours = M_Functions.LoadOptimizedImage("block_hours.png");
block_names_detected = M_Functions.LoadOptimizedImag("block_names_detected.png");
block_hours_detected = M_Functions.LoadOptimizedImag("block_hours_detected.png");
block_rect_names.x = x;
block_rect_names.y = y;
block_rect_names.w = block_surface_names -> w;
block_rect_names.h = block_surface_names -> h;
block_rect_hours.x = block_rect_names.x + block_rect_names.w;
block_rect_hours.y = block_rect_names.y;
block_rect_hours.w = block_surface_hours -> w;
block_rect_hours.h = block_surface_hours -> h;
}
//copy
Block::Block(const Block& source)
{
block_surface_names = source.block_surface_names;
block_surface_hours = source.block_surface_hours;
block_names_detected = source.block_names_detected;
block_hours_detected = source.block_hours_detected;
}
Block::~Block(){
//having this is necessary obviously- crashes program
//removing this causes the program not to crash
SDL_FreeSurface(block_surface_hours);
SDL_FreeSurface(block_surface_names);
SDL_FreeSurface(block_hours_detected);
SDL_FreeSurface(block_names_detected);
}
//where the object with SDL_FreeSurface() in the dtor is added to vector - crash!
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 using copy constructor
BlockVector.push_back(Block (Block(100,100, M_Functions)));
}
}
'shared_ptr'?爲什麼要添加那麼多開銷?爲什麼不告訴他如何讓'Block'移動? –
會有shared_ptr修復崩潰? – starhacker
@MooingDuck是可移動的,這裏是個好主意 – marcinj