Recent

Author Topic: Can't delete file after writing  (Read 1806 times)

jave

  • New member
  • *
  • Posts: 7
Can't delete file after writing
« on: October 15, 2018, 08:42:28 am »
Hi all,

Newbie here so please be gentle. :-)

I've written some code to do some very basic edge detection.  I can load the bitmap file successfully using
Code: Pascal  [Select]
  1. BitMap1 := Graphics.TBitMap.create;
  2. Bitmap1.Loadfromfile(Edit1.Text);

I then process the image and save to a file using

Code: Pascal  [Select]
  1. BitMap1.SaveToFile(Edit2.Text);

I then exit the program.  Problem is, when I try to delete the saved image file from Windows, I'm unable to.  Any ideas?  I think it's something to do with the file still being open, but not sure.  Thanks!

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: Can't delete file after writing
« Reply #1 on: October 15, 2018, 09:03:19 am »
Bitmap1.free would solve that probably, or closing your application first.
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

jave

  • New member
  • *
  • Posts: 7
Re: Can't delete file after writing
« Reply #2 on: October 15, 2018, 09:32:45 am »
Ok, thanks!  Where do I add "BitMap1.Free" - after saving?

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: Can't delete file after writing
« Reply #3 on: October 15, 2018, 11:36:09 am »
No idea. You didn't show us your code. I don't know if you created the bitmap in code for example...
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

jave

  • New member
  • *
  • Posts: 7
Re: Can't delete file after writing
« Reply #4 on: October 15, 2018, 11:53:14 am »
No problem. See code below.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   If FileExists(Edit2.Text) then begin
  4.      Label6.Caption := 'File exists. Enter new filename.';
  5.   end
  6.   else begin
  7.        BitMap1.SaveToFile(Edit2.Text);
  8.        BitMap1.Free;
  9.   end;
  10. end;
  11.  
  12. procedure TForm1.Button2Click(Sender: TObject);
  13. begin
  14.   BitMap1 := Graphics.TBitMap.create;
  15.   Bitmap1.Loadfromfile(Edit1.Text);
  16.   Label6.Caption := 'File loaded.';
  17. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 8171
Re: Can't delete file after writing
« Reply #5 on: October 15, 2018, 02:45:45 pm »
Looks fine to me. Still having problems? If you are repeating the operation you may try - just for debugging - FreeAndNil(Bitmap1).
If that works there is a bug in the bitmap sources, because it shouldn't be necessary: after the internal filestream is released, the file should not have any locks.
« Last Edit: October 15, 2018, 02:51:40 pm by Thaddy »
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7224
Re: Can't delete file after writing
« Reply #6 on: October 15, 2018, 03:05:15 pm »
How long after do you test? It could be an antivirus that is still keeping it locked.

Usually locks don't immediately disappear after a write (or directory mutation) operation, but usually they go away in 100ms of ms. If it is more, it is usually the antivirus.

jave

  • New member
  • *
  • Posts: 7
Re: Can't delete file after writing
« Reply #7 on: October 16, 2018, 11:22:31 am »
Hmmm.. program now works as expected.  Thanks to all for your help!   8)