🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.tech
Article #8457

Re: More suggestions for programmers

#8457
From: John Russell
Date: Sun, 10 Apr 1988 18:16
60 lines
2481 bytes
In article <8804060758.AA14048@jade.berkeley.edu> SLMYQ@USU.BITNET writes:
>Here's another suggestion for programmers.  It basically has to do with
>freeing resources you've allocated, EVEN WHEN THE PROGRAM HAS FAILED.
>
>The majority of programs I've seen just do something like this:
>
>main()
>{
>        if ((Screen = OpenScreen(&NewScreen)) == NULL)
>                exit(1);
>
>        if ((Window = OpenWindow(&NewWindow)) == NULL)
>                exit(2);
>
>Although one solution would be to have every allocation call, on an error,
>have all the commands needed to free everythings that was allocated before.
>However, this quickly gets not-so-fun:
>
>You get the point.  Now here's the solution I always use.
>
[ example of Finish() which checks all resources and frees if they have been
  allocated ]

This is a good point. People using Manx will probably want to call this routine
_abort() so that it will be automatically called on ^C interrupt. Then you
can check for ^C yourself or just do periodic calls to the Manx printing
functions (which do it themselves).

I've used this approach to allow aborts which happened while the WB screen
was dual-playfield and had a picture halfway loaded in, or windows belonging
to other applications were locked and having their contents dumped to the
printer, without difficulty.

Things to watch for:

- if you lock layers, have a boolean variable (eg layers_locked = FALSE) that
  lets your _abort() routine know it has to unlock them; some resources may
  not have a pointer associated with them
- make sure everything is initialized to NULL, FALSE, etc. Whatever state
  shows that it isn't currently in use.
- make sure when you free up something inside the program you reset the state
  of that resource to be false, eg

fp = fopen(...)
[ read & write ]
fclose(fp);
fp = NULL;

...

_abort()
{
	if (fp) fclose(fp);
...
}

John
-- 
"Bagpipers are prone to lung infections due to fungus that grows inside the bag"
				- Time magazine brightens my day
Technical Details
Message-ID: <4651@garfield.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!garfield!john13
References: <8804060758.AA14048@jade.berkeley.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!garfield!john13
From: john13@garfield.UUCP (John Russell)
Newsgroups: comp.sys.amiga.tech
Subject: Re: More suggestions for programmers
Message-ID: <4651@garfield.UUCP>
Date: 10 Apr 88 18:16:13 GMT
References: <8804060758.AA14048@jade.berkeley.edu>
Reply-To: john13@garfield.UUCP (John Russell)
Organization: Memorial University of Newfoundland, St. John's
Lines: 60