Sudden Loss of access to files

I have an application that checks for files create by a Point of Sale application and then imports these files. It also compares the files and write to a settings file what the last time was it imported a file.

I have a problem where my application runs fine for a random amount of time (normally between 2-5days), then all of a sudden my application loses the ability to write to any file in Windows, even files created by my application and files that it was able to access just moments ago.

If you restart the application, everything works fine again for a random amount of time.

This happens on Windows 7/8/8.1/10/ server 2012

Code to write to the File:
function TFrmMain.CompareFileDate(sFileName : string): Boolean;
var
  dOldDate  : TDateTime;
  dNewDate  : TDateTime;
  ini       : TIniFile;
  iFileDate : Integer;
begin
  try
    //Get Old File Date
    ini := TIniFile.Create(IniFileName);
    try
      dOldDate := ini.ReadDateTime('Import','LastImportedFile',0);

      //Get File Date and Time
      iFileDate := FileAge(sFileName);
      dNewDate  := FileDateToDateTime(iFileDate);

      if dNewDate > dOldDate then begin
        Result := True;
        ini.WriteDateTime('Import','LastImportedFile',dNewDate);
      end else
        Result := False;
    finally
      FreeAndNil(ini);
    end;

  except
    on e : Exception do begin
      Result := True;
    end;
  end;
end;

Open in new window

Werner EngelbrechtSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
what excpetion do you get ?

you might want to add that info to a log file:

except
  on E: exception do 
  begin
    AddLog(Format('Error writing to inifile "%S" : %s', [IniFileName, E.Message]));
    Result := False;
  end;
end;

Open in new window

 
ugh ?? result = true inside the exception ???
what are you doing with the result of this function ?

the function returns true when the file is newer and when you get an exception
and false when the file is older or the same

this q contains a unit with an addlog, which keeps log files small for easy opening and searching
https://www.experts-exchange.com/questions/24723823/Handling-exceptions-hide-exception-from-users-create-log-file-of-exception.html#25307590
Werner EngelbrechtSoftware DeveloperAuthor Commented:
Sorry for the Incomplete Information.

I have a Logging unit in the application which i removed to make the code more "generic". The error is logged in the Except.

The "Result := True" is there so that the application still process the file in the event of an error, this will not create duplicate data, the compare is just there to prevent unnecessary processing.

Error Message in my Log FIle : " Unable to write to C:\Users\Public\Documents\{CompanyName}\{ApplicationName}\ServConf.Sdb"
Geert GOracle dbaCommented:
.sdb is a database ?

does it go offline ?
does that give any messags ?

or is it just a flat file

you don't have  quota set on the folders ?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Werner EngelbrechtSoftware DeveloperAuthor Commented:
.sdb is TInifile with a different extension.

This procedure is one of a few that gets the Error, all of them access one or another file that they could access before.
Another note: when this access problems starts, I also loose the ability to load or access DLLs, dlls that work file if i can write to the other files

The exception is the "Unable to write to C:\Users\Public\Documents\{CompanyName}\{ApplicationName}\ServConf.Sdb"

No Quota, the application works immediately after restarting the application.
Geert GOracle dbaCommented:
sounds like a deadlock
or a wait until the file is released ...

your code is working with threads ?
if so, you'll have to protect access to the file with a crtical section (like in the ulogging sample of the q i posted in previous comment)

if it's not working with threads, then maybe you have a procedure which does not release the file
difficult to find ...
if you don't have too many locations which open/write/read from a file, then i would add extra logging in a specific file, which contains all the file accesses

function TFrmMain.CompareFileDate(sFileName : string): Boolean;
var
  dOldDate  : TDateTime;
  dNewDate  : TDateTime;
  ini       : TIniFile;
  iFileDate : Integer;
begin
  try
    //Get Old File Date
    AddLog(Format('Opening "%s" by proc %s", [IniFileName, 'CompareFileDate']), true, 'fileaccess');
    ini := TIniFile.Create(IniFileName);
    try
      dOldDate := ini.ReadDateTime('Import','LastImportedFile',0);

      //Get File Date and Time
      iFileDate := FileAge(sFileName);
      dNewDate  := FileDateToDateTime(iFileDate);

      if dNewDate > dOldDate then begin
        Result := True;
        ini.WriteDateTime('Import','LastImportedFile',dNewDate);
      end else
        Result := False;
    finally
      FreeAndNil(ini);
      AddLog(Format('Closed "%s" by proc %s", [IniFileName, 'CompareFileDate']), true, 'fileaccess');
    end;

  except
    on e : Exception do begin
      Result := True;
    end;
  end;
end;

Open in new window


in the the fileaccess log you'll probably get 2 open instructions following each other
it requires some work, and i would also use a parameter to switch on or off this extra logging

but as it only happens after 2-5 days, it's a difficult problem to find
Werner EngelbrechtSoftware DeveloperAuthor Commented:
I will try the above, but I do not think that is the problem, seeing as different procedures get the same error on different files. As soon as 1 procedure gets this error, all the other procedures has the same error no matter the file or procedure.

As mentioned in the other post, I even get errors with loading DLLs
Geert GOracle dbaCommented:
you dynamically load and unload the dll's ?
or you only load them once ?
Werner EngelbrechtSoftware DeveloperAuthor Commented:
Load and Unload as needed
Geert GOracle dbaCommented:
since it's always different files
have you checked for leaking handles ?
https://blogs.technet.microsoft.com/markrussinovich/2009/09/29/pushing-the-limits-of-windows-handles/

with process explorer you can list the open handles of a program
Right click and select properties and the performance tab

if you see the handles continually going up, then you have a leaking handles problem
Werner EngelbrechtSoftware DeveloperAuthor Commented:
I have not checked that yet.

Thank you. Will give this a try and revert back on if this was the problem
Geert GOracle dbaCommented:
if you do have a leak of some kind, it will probably be the memory which hits the limit first
a 32-bit program usually hits bottom at 1.78Gb of ram

the event viewer might also hold a clue to errors
Werner EngelbrechtSoftware DeveloperAuthor Commented:
Waited till today to check for the memory or handle leak, nothing. Handles and people usage stay constant.

Back to square 1.
Geert GOracle dbaCommented:
the most difficult problems to find are the ones which aren't there

i can only recommend to add logging to the place where you "think" the problem can be
you could add logging everywhere, but then that would result in looking for the needle in the haystack
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.