[fpc-pascal]help with pointers?

Marco van de Voort Marcov at stack.nl
Sun Oct 21 15:59:03 CEST 2001

> >This means you must make sure your program can keep track in one way or
> >another of which pointers are already initialized and when they have to be
> >freed. There is no magic trick to get around this, unless you switch to
> >environment in which all memory management is handled for you by the run
> >time system (such as Java).
> The best advice is to always initialize your pointers to nil before using 
> them, and set them back to nil after you have deallocated the memory they 
> point to (the second part is necessary in Delphi, please correct me if I'm 
> wrong.)

There are two distinct reasons to set them back to nil.

- One should set _all_ pointers ack to NIL in windows, because NIL 
is in  protected space in Windows, and will trigger an exception if it 
is defereferenced.

- Certain automatic functionality in Delphi needs _some_ values to 
be NILled after use.

On other systems, other values may apply btw. That's why it is good 
to have freeandnil (though I don't like the name, since it seems to 
imply zero). If one needs another OS (or windows versions) to set 
old unused pointers to $FFFFFFFF in order to have the processor 
detect a violation, only freeandnil must be adjusted.

One can even extend that further, and have two different libraries. 
E.g. the debugging library FreeAndNill resets the pointer to an easy 
to detect value like $DEADBEEF

More information about the fpc-pascal mailing list