2015-01-10 13 views
1

좋아, 화면에서이를 업데이트하고 반영 할 수있는 정수를 표시하려고했습니다. (e.x. 마우스 위치의 x와 y 좌표를 보여줌), 나는 원했던대로 실행했지만, 좋은 ol'task 관리자를 열었고 7 분 동안 실행 한 후 약 600MB에 도달했다. 화면에 렌더링 한 정수를 주석 처리했을 때 (그리지 않거나 업데이트하지 않도록) 이 프로그램은 7 분 후에 약 20MB의 용량을 나타 냈습니다. 나는 문제를 찾는데 실패했다. 누군가 내가 잘못 갔는지 알아낼 수 있을까? 감사.SDL_TTF 메모리 누수 문자열을 그리는 중입니까?

TextManager.cpp

void textManager::intToString(Uint32 number, int x, int y) 
{ 
    ss << number; 
    text = ss.str().c_str(); 
    surface = TTF_RenderText_Blended(font, text.c_str(), color); 
    if (surface == NULL) 
     cout << "surface is NULL \n" << SDL_GetError() << "\n"; 
    texture = SDL_CreateTextureFromSurface(Gfx::Instance()->getRenderer(), surface); 
    SDL_FreeSurface(surface); 
    src.x = 0; 
    src.y = 0; 
    src.w = dst.w = surface->w; 
    src.h = dst.h = surface->h; 
    dst.x = x; 
    dst.y = y; 

    SDL_QueryTexture(texture, NULL, NULL, &src.w, &src.h); 
    ss.str(std::string().c_str()); 

} 

void textManager::draw() 
{ 
    SDL_RenderCopy(Gfx::Instance()->getRenderer(), texture, &src, &dst); 
} 


textManager::~textManager() 
{ 
    if (texture != NULL) 
     SDL_DestroyTexture(texture); 
    font = NULL; 
    surface = NULL; 
    ss.clear(); 
    delete surface; 
} 

답변

0

당신이 TTF_RenderText_Blended로 표면을 만든 다음 그 표면에서 텍스처를 만들 textManager::intToString 호출 할 때마다의 //TextMangager.h

#pragma once 
#ifndef TEXTMANAGER_H 
#define TEXTMANAGER_H 

#include <SDL_ttf.h> 
#include "graphicsManager.h" 
#include <sstream> 

struct textManager{ 
public: 
    textManager(){} 
    ~textManager(); 
    void SetText(std::string msg, int x, int y); 
    void draw(); 
    void setFont(std::string filepath, int size); 
    void changeFontSize(int size); 
    int getWidth(); 
    int getHeight(); 
    void setColor(int r, int g, int b); 
    void intToString(Uint32 number, int x, int y); 

private: 
    TTF_Font*font; 
    SDL_Texture*texture; 
    SDL_Rect dst; 
    SDL_Rect src; 
    SDL_Surface*surface; 
    SDL_Color color; 
    std::stringstream ss; 
    std::string text; 


}; 

#endif // TEXTMANAGER_H 

// 부.

표면을 자유롭게하고 있지만 질감에 대해 동일한 작업을 수행하지 않은 것처럼 보입니다. 즉, 모든 프레임에서 텍스처 포인트가 다시 할당되지만 메모리는 계속해서 메모리 누수가 발생하지 않습니다.