How to reproduce?
The broken file is full of zeros. From its size, it does not seem to take long to have this happen.
Does gsRealm change, or is it a fixed value?
Can you save sSeccion, sKey and sValue in another file to track the sequence of values needed to cause this?
Your code seems to catch any exception, but does not report them. Is that intentional?
Finally, the usual basic info: Laz/FPC versions, OS, GUI or console app?