I’m seeing what appears to be a very small memory leak when I call s = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0); repeatedly. If I call s = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, 0) there is no leak, but also I don’t see the surface I need either, which is what we would expect with a 0 height and 0 width surface.
I have a Sprite object which contains a Timer object and the Timer object contains a ProgressBar object. The ProgressBar uses a surface to draw a color over the same area as the Sprite. This is basically a debug feature I’m using to track the progress of a timer.
When I create a Sprite/Timer/ProgressBar object over and over again I see this leak.
I create a Sprite below and I only see the leak if I recreate hundreds of sprites. Also note that each time I create a new sprite I’m seeing the destructor for Sprite getting called so I’m assuming the old surface is getting cleaned up each time.
Any ideas on why I would see a leak in this case?
Please also let me know if you need more context.
spriteBackground = ley::Sprite(TextureManager::Instance()->getTexture(background_level.c_str()), 0, {start_rect});
ley::Sprite::Sprite()
:
Renderable(),
fader(1000,{0,0,0,0}) {
SDL_Log("Sprite() called");
}
ley::Sprite::Sprite(SDL_Texture * t, unsigned int s, std::vector<SDL_Rect> v)
:
Renderable(),
animSpeed(s),
texture(t),
fader{1000,{0,0,0,0}} {
SDL_Log("Sprite(SDL_Texture * t, unsigned int s, std::vector<SDL_Rect> v) called");
if (!texture) {
return;//EARLY EXIT
}
bool multiframe = false;
if(v.size() > 0) {
fader(1000,{v.at(0)}); // fader is a Timer
}
multiframe = v.size() > 1;
SDL_Rect source_rect;
dest_rect.y = source_rect.y = 0;
if(multiframe) {
for(auto rect : v) {
source_rect.x = rect.x;
source_rect.y = rect.y;
source_rect.w = dest_rect.w = rect.w;
source_rect.h = dest_rect.h = rect.h;
frames.push_back(source_rect);
}
} else {
SDL_QueryTexture(texture, NULL, NULL, &source_rect.w, &source_rect.h);
dest_rect.w = source_rect.w;
dest_rect.h = source_rect.h;
dest_rect.x = source_rect.x = 0;
}
}
ley::Timer::Timer(unsigned int m, SDL_Rect rect)
: Renderable(),
mili(m),
active(true),
expired(false),
sdlTimerReady(true),
progressBar(rect) {
if(SDL_Init(SDL_INIT_TIMER) >= 0) {
// if all good
} else {
printf("Can't Initialize SDL2 Timer");
sdlTimerReady = 0;
}
}
ley::ProgressBar::ProgressBar(SDL_Rect rect)
: Renderable(),
border(rect),
progress(border) {
s = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0); //This seems to cause the leak
SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 255, 0, 0));
}
ley::ProgressBar::~ProgressBar() {
SDL_DestroyTexture(t);
SDL_Log("~ProgressBar() dtor called");
}