Mail Archives: djgpp/1999/09/12/11:48:32
Krogg wrote:
>
> Sorry if this is long winded but i dont know
> how to explain it in a shorter way......
>
> I wanted to get a screen shot from a
> program i wrote yesterday.It draws
> a random IFS type fractal on the screen
> and if you press the [space] it clears the
> screen and draws another else it exits.
> I knoticed that pressing [print screen]
> would not capture the screen so i made the
> else part see if the key pressed was "c"
> and if so do this:
>
> save_pcx("ifs.pcx",screen,pallete);
>
> But after looking at the .pcx file i knoticed that
> it was not only the screen but the whole virtual
> screen that was saved....not exactaly what i wanted
> to happen...I read the allegro docs and it said
> try this instead:
>
> ===========begin snip from allegro docs===================
>
> BITMAP *bmp;
> PALETTE pal;
>
> get_palette(pal);
> bmp = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
> save_bitmap("dump.pcx", bmp, pal);
> destroy_bitmap(bmp);
>
> =============end snip from allegro docs===================
>
> Well,I am lazy so i decided to try this instead:
>
> save_pcx("ifs.pcx",create_sub_bitmap(screen, 0, 0, SCREEN_W,
> SCREEN_H),pallete);
>
> What a shortcut!...and,it worked great!
>
> Is there anything wrong with doing stuff like this?
> I mean,it worked,but is there some reason i shouldnt
> do this kind of thing?
It's a memory leak. `destroy_bitmap' frees the memory used to store the
bitmap, and your version makes it impossible to do that (the value of
bmp is lost, so you can't find it to destroy it). Not freeing your
resources is considered bad programming practice. If this code can only
happen once, or a small fixed number of times, it may not be a real
problem, since all your memory will be freed when the program quits.
However, if it's done repeatedly, then the program will eventually run
out of memory and die; not a good thing.
--
Nate Eldredge
neldredge AT hmc DOT edu
- Raw text -