delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/12/11:48:32

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: c,allegro,coolness....
Date: Sat, 11 Sep 1999 00:42:47 -0700
Organization: Harvey Mudd College
Lines: 58
Message-ID: <37DA07F7.182B297C@hmc.edu>
References: <37D9D699 DOT 8BFA4EF6 AT gtcom DOT net>
NNTP-Posting-Host: mercury.st.hmc.edu
Mime-Version: 1.0
X-Trace: nntp1.interworld.net 937035761 76102 134.173.45.219 (11 Sep 1999 07:42:41 GMT)
X-Complaints-To: usenet AT nntp1 DOT interworld DOT net
NNTP-Posting-Date: 11 Sep 1999 07:42:41 GMT
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.12pre4 i586)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019