Re: TIniFile crash

Rafael Picanço via fpc-pascal <[email protected]>
Newsgroups gmane.comp.compilers.free-pascal.general
Message-ID <CAGpuCi6p2UyuGNfr-eHrdLzNCv-sROoc=o3_XDC11D=tvp01eg@mail.gmail.com>
Hi James, here are some routines to a load-create/archive cycle using a
temporary TIniFile:

program handle_inifile;

uses Classes, SysUtils, IniFiles,
FileUtil;// In Lazarus you will need the LazUtils package to see the
FileUtil unit

procedure ArchiveIniFileTo(var AIniFile : TIniFile; ADestination : string);
begin
  if Assigned(AIniFile) then begin
    { You may want to implement a method
      to update/invalidate in-memory stuff here }
    //IniFileDescendent.Invalidate;
    AIniFile.UpdateFile; // Flush data to disc

    CopyFile(AIniFile.FileName, ADestination);
    if FileExists(AIniFile.FileName) then begin
      DeleteFile(AIniFile.FileName);
    end;
  end;
end;

function LoadIniFile(AFilename: string) : TIniFile;
begin
  Result := TIniFile.Create(AFilename);
  Result.CacheUpdates := True;
end;

var
  TemporaryIniFile : TIniFile;

begin
  TemporaryIniFile := LoadIniFile('tmp.ini');
  try
    TemporaryIniFile.WriteString('Section', 'Key', 'Value'); // in memory
cached updates
     ArchiveIniFileTo(TemporaryIniFile, 'destination.ini');
  finally
     TemporaryIniFile .Free;
  end;
end.

I use this approach because in-memory manipulation of the ini file is what
I need (with the bonus of being faster and safer).

Best,
Rafael Picanço

On Thu, Jul 31, 2025 at 10:39 AM <[email protected]>
wrote:

> Send fpc-pascal mailing list submissions to
>         [email protected]
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> or, via email, send a message with subject or body 'help' to
>         fpc-pascal-request-PD4FTy7X32k2wBtHl531yWD2FQJk+8+b@public.gmane.org
>
> You can reach the person managing the list at
>         fpc-pascal-owner-PD4FTy7X32k2wBtHl531yWD2FQJk+8+b@public.gmane.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of fpc-pascal digest..."
>
>
> Today's Topics:
>
>    1. Re:  TIniFile crash (Michael Van Canneyt)
>    2.  Broken Links on Free Pascal Wiki (Tim Coates)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 30 Jul 2025 16:25:51 +0200 (CEST)
> From: Michael Van Canneyt <michael-y8GAwIkqPAJpn7uciFS/[email protected]>
> To: FPC-Pascal users discussions <[email protected]>
> Subject: Re: [fpc-pascal] TIniFile crash
> Message-ID: <1c5e79f3-5698-690b-b7be-7900484ba51c-y8GAwIkqPAJpn7uciFS/[email protected]>
> Content-Type: text/plain; charset="utf-8"; Format="flowed"
>
>
>
> On Tue, 29 Jul 2025, James Richters via fpc-pascal wrote:
>
> > I will try just creating a temp file, and once it?s completed delete the
> original file then rename the temp file to the correct name, it?s a good
> idea, and a least I will now that nothing I am doing is trying to access
> the file? because I can generate some random name for the temp file,  but I
> guess it wouldn?t stop something like antivirus from trying to scan it
> before I?m done.   It?s a difficult problem to figure out because it
> happens very rarely, but it does happen enough to be annoying.
> >
> > I put the delete and wait for it to be gone procedure just to make sure
> the file was really gone before trying to create a new one in an attempt to
> solve this issue, and it is well past that part of the code when the crash
> happens, it had already written 19 entries to the new  ini file when it
> crashed, and that?s why I?m confused, the error is about creating the file,
> but it was already created and there were already 19 successful entries to
> it? so what?s it creating at the point of the crash?
> >
> > The thing is that the file that is on the disk after the crash is only a
> partial file, up to the exact line that crashed, so the file must have
> finished the delete and there was no problem creating a new file and
> writing to it.
> >
> > I am having a suspicion that Ini.WriteString is opening the file,
> writing to it then closing it, which makes no sense to me, I would think
> that ini.create would create the file and open it, and it would remain open
> until ini.free..  but the fact that it sometimes gives me this message:
> >
> > EFCreateError: Unable to create file
> "I:\Programming\Gcode\Mill\Location.ini": The process cannot access the
> file because it is being used by another process.
> >  $00603277
> >
> > After writing successfully 19 entries to the ini files seems to indicate
> > that it?s opening and closing the file for each Ini.WriteString and that
> > 1% of the time the OS didn?t finish writing the file before it was being
> > opened again?  I?m just guessing here about what could cause this.
>
> The TIniFile indeed writes to disk after every writeNNN()
>
> There are 2 possibilities to change this behaviour:
>
> - Set CacheUpdates to true, and call UpdateFile when done.
>
> - Use TMemInifile, and call UpdateFile when done.
>    (TMemIniFIle simply sets CacheUpdates to true in its constructor)
>
> This should improve matters.
>
> You can also combine this with the "use temp filename and rename" approach.
>
> Michael.
>
> ------------------------------
>
> Message: 2
> Date: Thu, 31 Jul 2025 17:50:57 +1000
> From: Tim Coates <[email protected]>
> To: FPC-Pascal users discussions <[email protected]>
> Subject: [fpc-pascal] Broken Links on Free Pascal Wiki
> Message-ID:
>         <
> CAPGcr8-zO1-SEeSiNxXXTeO5HcwsY+y1ndq+vKdAq3Lj0RRVuA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
> Content-Type: text/plain; charset="utf-8"
>
> Dear Free Pascal Team,
>
> I understand and appreciate that Free Pascal is a volunteer-driven project,
> and I?m grateful for all the work that has gone into it over the years.
>
> While researching style guides for something I?m working on, I went to the
> Free Pascal wiki to look for documentation. Unfortunately, both the links I
> found for the style guide and the project guidelines appear to be dead. I
> was hoping to refer to these resources, but couldn?t access them. I could
> otherwise Delphi 11 based guide?
>
> Out of curiosity, I also wondered whether there might be other important
> pages that have gone offline or been moved. Is there an updated location
> for these documents? And if there?s a way I could help with checking or
> updating wiki links, I?d be happy to pitch in.
>
> Thanks again for your time and for maintaining such a valuable tool for the
> Pascal community.
>
> Best regards,
> Tim Coates
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20250731/1a5bb934/attachment-0001.htm
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> fpc-pascal maillist  -  [email protected]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
>
> ------------------------------
>
> End of fpc-pascal Digest, Vol 253, Issue 16
> *******************************************
>
>

_______________________________________________
fpc-pascal maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.