[fpc-pascal] A warning when Blockwriting/reading dynamic array pointers

J├╝rgen Hestermann juergen.hestermann at gmx.de
Fri Apr 1 18:04:56 CEST 2011


I just wanted to post a warning for everybody who wants to 
(block)write/read dynamic arrays to/from file. All the documentation 
about dynamic arrays I found only told me what I *can* do with them but 
none said what I can *not* do. And there was no detailed information 
about the internal works. That left room for (mis)interpretation and 
costed me many days of headache. Maybe my experience is of interest for 
others (and maybe it can be added to the documentation):

If you Blockwrite a dynamic array pointer to file (i.e. because it is 
part of a large record) and read it back later with Blockread then the 
pointer value is invalid of course. BUT, if you now try to set it to nil 
as in

DynArray := nil;

then Free Pascal seems to free the memory where the pointer points to! 
This creates hard to find access violations later in your program. This 
cost me days of headache because never in my life I would have expected 
that a simple assignment with nil would do more than just setting bytes 
to zero. Instead of the above assignment I had to use

fillchar(DynArray,sizeof(DynArray),#0);

This solved all problems.




More information about the fpc-pascal mailing list