[fpc-pascal] TIniFile crash/memory loss

Bart bartjunk64 at gmail.com
Fri Apr 1 00:17:30 CEST 2011


On 3/31/11, José Mejuto <joshyfun at gmail.com> wrote:
> B> This however means that the programmer has to protect TIniFile.Free
> B> with a Try..Except block, which is also rather unusual.
>
> It's unusual, but there is an error, or the class does not perform the
> save in the free procedure and uses a "flush" (forced) or the class
> must raise an exception as the expected job has not been performed.

The job of the destructor, however, is to clean up the object and return memory.
This should never fail IMHO.

One could even argue that the whole call to UpdateFile in the
destructor simply should not be there.
If the user (programmer) takes the risk of caching the write updates,
then it is the programmers responsability to make sure the cached
values are written at some point.

Tipically I would do something like

begin
  try
    Ini := TIni.Create(AFilename);
    try
      Ini.CacheUpdates := True;
      Ini.WriteString(SomeSection, SomeIdent, SomeValue);
      //lots of writing
      Ini.UpdateFile; //flush bugger to file
    except
      ShowMessage('Unable to write to config file');
    end;
  finally
    Ini.Free;
  end;
end;

I would the expect that all (writing) errors are trapped, and the
finally statement ensures cleaning up.

I have come to understand that Delphi does it in such a way.
If writing fails, the destructor is still (silently) done.

Bart



More information about the fpc-pascal mailing list