On Wed, 25 Apr 2001, G.W. Haywood wrote:

> Hi all,
>
> On Wed, 25 Apr 2001, Remco Schaar wrote:
>
> > It is very hard to write perfect code,
>
> True, but it's not hard to write code that doesn't leak memory.
>
> void *p = NULL;
> ...
> ...
> if( p ) { exit(POINTER_ERROR); }
> void *p = malloc(n);
> ...
> ...
> free( p );
> p = NULL;
> ...
> ...
>
> By which I mean that I use a certain set of pointers for memory
> allocation and nothing else.  The pointers are declared and
> initialized to NULL in the same statement.  Whenever I free some
> memory I set the pointer which was used to point to it back to NULL.
> Whenever I use a pointer for memory I first check that its value is
> NULL.  Then if I try to use it when it's already being used for
> something else my program tells me in a fairly unceremonious fashion.
>
> This is one of a number of defensive techniques I've developed over
> the years.  It's better to wrap this kind of stuff in a library of
> defensive memory management routines (I expect you can buy one:) as
> there's less room for errors and it saves a lot of typing.

Easier still, just use boehm gc. ;-)

(I've found boehm gc to not only be easier to work with, but also to be
*faster* than "hand" de-allocating memory)

-- 
<Matt/>

    /||    ** Founder and CTO  **  **   http://axkit.com/     **
   //||    **  AxKit.com Ltd   **  ** XML Application Serving **
  // ||    ** http://axkit.org **  ** XSLT, XPathScript, XSP  **
 // \\| // ** mod_perl news and resources: http://take23.org  **
     \\//
     //\\
    //  \\

Reply via email to