0

Environment: Delphi 2010, Indy10

I know I am not alone with the problem... File is Still in Use Error 32 How can I free it?

But after some days of fight I give up.

My code

 var
   multi: TIdMultipartFormDataStream;
   ss: TStringStream;
   FHTTP: TIdHTTP;

FHTTP := TidHTTP.Create(nil);
Multi:=TidMultiPartFormDataStream.Create;
Multi.AddFormField('eventid', TSettingsManager.GetAppSettings('EventID'));
Multi.AddFormField('password', TSettingsManager.GetAppSettings('EventPassword'));
Multi.AddFile('file', filename, 'image/jpeg'); //GetMIMETypeFromFile(fileName));
try
  FHTTP.Post(TSettingsManager.GetAppSettings('WEBServer') + '/upload', Multi, ss);
finally
  Multi.Clear;
  FreeAndNil(Multi);
  FreeAndNil(FHTTP);
end;

IOUtils.TFile.Delete(filename);

I have exception "The file is in use" when I try to delete the file. How should I act to get file free and then delete it?

Community
  • 1
  • 1
mad
  • 1,029
  • 3
  • 17
  • 38
  • Did you try with the latest Indy 10.6 version? Maybe it is fixed now. – mjn Sep 18 '14 at 12:39
  • @mjn: thank for the answer. I use Indy with gsIdVersion = '10.6.0.5146'; Will try to update now. – mad Sep 18 '14 at 12:57
  • @mjn: Now I have gsIdVersion = '10.6.1.5187'... The situation is the same. – mad Sep 18 '14 at 13:12
  • Thanks to everybody. My problem was in multithreading access to single file and very slow inet connection. – mad Sep 18 '14 at 14:26

1 Answers1

0

There is no error in the code shown.

Some potential memory leaks can be seen, which can be fixed easily with proper usage of try .. finally.

Also iirc you can create an instance of TIdHTTP with no owner in a shorter way. Instead of:

HTTP := TIdHTTP.Create(nil);

you can just leave away the argument:

HTTP := TIdHTTP.Create;
mjn
  • 36,362
  • 28
  • 176
  • 378