所以我在我的SDL2.0程序中有一個memeory泄漏 我設法把它弄到這塊代碼, 我不知道我是否只是盲目,但任何人都可以看到他們是否他們在這裏可以看到內存泄漏?SDL 2.0內存泄漏
我感謝任何迴應的人。
SDL_Color colour = { 255,255,255 };
SDL_Surface* surfaceMessage = nullptr;
SDL_Texture* Message = nullptr;
for (int i = 0; i < m_vItems.size(); i++)
{
if (surfaceMessage != nullptr)
{
SDL_FreeSurface(surfaceMessage);
}
SDL_Color colour = {255,255,255};
if (i == m_iSelection)
{
colour = { 255,0,0 };
}
surfaceMessage = TTF_RenderText_Solid(m_Font, m_vItems[i].c_str(), colour); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first
Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); //now you can convert it into a texture
//Get split change
float thing = screenHeight - 2 * (screenHeight* 0.3);
thing /= m_vItems.size();
SDL_Rect Message_rect; //create a rect
Message_rect.x = m_iPosX; //controls the rect's x coordinate
Message_rect.y = m_iPosY + (i * thing); // controls the rect's y coordinte
Message_rect.w = 0.18* screenWidth; // controls the width of the rect
Message_rect.h = 0.08 * screenHeight; // controls the height of the rect
SDL_RenderCopy(renderer, Message, NULL, &Message_rect); //you put the renderer's name first, the Message, the crop size(you can ignore this if you don't want to dabble with cropping), and the rect which is the size and coordinate of your texture
}
if (surfaceMessage != nullptr)
{
SDL_FreeSurface(surfaceMessage);
}
'Message'是否需要釋放內存? – NathanOliver
@NathanOliver紋理確實需要釋放內存,儘管該函數在技術上被命名爲SDL_DestroyTexture(https://wiki.libsdl.org/SDL_DestroyTexture)。 OP可能是使用'std :: unique_ptr'儘管 – jaggedSpire
@jaggedSpire Cool最好。我從來沒有使用過SDL,因爲它只是一個猜測,因爲它是一個指針,並且浮出水面正在清理。 – NathanOliver