SDLx::Surface->blit() Memory Issue Workaround
[email protected] (Sheppy R)
| Newsgroups | perl.sdl.devel |
|---|---|
| Message-ID | <[email protected]> |
I did some experimenting with blitting Surfaces. I think the main memory leak I've been seeing is coming from the creation of a new SDLx::Rect every time the Rect coordinates are passed as [x, y, h, w]. By comparing 5 different blits, I was able to find out that by declaring the SDLx::Rect once and reusing it you can avoid this particular memory leak. I originally was working with a single background SDLx::Surface ($background) that has a white rectangle drawn on it. I would then blit this image to the SDLx::App ($app) during the Show Handler. my $rect = SDLx::Rect->new(0,0,200,200); Tests $background->blit($app) = Steady memory utilization $background->blit($app, [0,0,200,200]) = Steady increase in memory utilization (~20K/second) $background->blit($app, [0,0,200,200], [0,0,200,200]) = Steady increase in memory utilization (~30K/second) $background->blit($app, $rect) = Steady memory utilization, dropped 4K after a minute or two $background->blit($app, $rect) = Steady memory utilization, shallow fluctuation of +/- 4K ever few minutes In the attached code, I've commented out 6 test lines (the blit($app) is duplicated for easy reference) at lines 32-34 & 46-48. I don't have any fancy way of testing the memory utilization, I just watched the Windows Task Manager for Memory changes on perl.exe (and counted seconds in my head...) Hopefully, I'll have time at work tomorrow to test this out with the Pong game from the SDL manual. This is kind of a roundabout way of getting there, but the extra code in creating the background surface and the rectangle really seem to help out.
MemoryTester.pl
(application/octet-stream, 1.2 KB)
#!usr/bin/perl
use strict;
use warnings;
use SDL;
use SDLx::App;
use SDLx::Rect;
my $app = SDLx::App->new(
h => 200,
w => 200,
exit_on_quit => 1,
);
my $background = SDLx::Surface->new(
h => 200,
w => 200,
);
my $rect = SDLx::Rect->new(0,0,200,200);
$background->draw_rect($rect, 0xFFFFFFFF);
$app->add_show_handler(sub {
################################################
#
# These three lines give an indication of the
# memory leak for blitting surfaces to be in the
# coordinate arrays.
#
################################################
#$background->blit($app);
#$background->blit($app, [0,0,200,200]);
#$background->blit($app, [0,0,200,200], [0,0,200,200]);
################################################
#
# These three lines give an indication that by
# predefining the $rect coordinates, the
# SDLx::Rect->new() is not called every time.
# This seems to stabilize the memory utilization.
#
################################################
#$background->blit($app);
#$background->blit($app, $rect);
#$background->blit($app, $rect, $rect);
$app->update;
});
$app->run();