delete FileStream problem, please help

Hello,

I have a loop downloading files from web:

.......
SaveFileName = SavePath + SaveFileDataSet + ForecastLength->Text + ".grb";
fs = new TFileStream(SaveFileName, fmCreate);
Download1->Get(DownloadLink, fs);
delete fs;
........

I also created Cancel button where I disconnect: Download1->Disconnect()
When I try to delete fs after clicking Cancel, I get Access violation at adr. xyz, Read of adr. xyz....

and the file being downloaded and saved is then locked by that process, so when I try to create the file with the same name, I get Cannot create file, it is used by anothr process....

Could you please guide me how to delete the stream and unlock the file....

Your fast response is highly appreciated.

Thank you.

Marian
You'll have to post the relevant parts of your code.
The loop after clicking on Download button:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void __fastcall TForm1::DownloadBtn1Click(TObject *Sender)
{

dwncounter = (dwnend - dwnstart) + 1;
numofdwn =  dwnstart;
SetCurrentDir(Path);


Display1->Clear();
Display1->Lines->Add("Requested data set is " + DataSetStr);
Display1->Lines->Add("Requested model run date is " + DownloadDate);
Display1->Lines->Add("Requested model run time is " + DownloadTime + "Z");
Display1->Lines->Add("Requested forecast length is " + IntToStr((dwnend - dwnstart)*3) + " hours");
Display1->Lines->Add("Requested forecast step is " + FcstStepStr + " hours");
Display1->Lines->Add("Requested number of files is " + IntToStr(dwncounter));
Display1->Lines->Add("-------------------------------------------------------------------------");
Display1->Text = Display1->Text + ("Starting download...\n\r");



for (numofdwn; numofdwn < dwncounter; numofdwn=numofdwn+FcstStep/3) {
	ForecastLength->ItemIndex = numofdwn;
	DownloadLink = URLprefix + DownloadTime + URLsuffix + ForecastLength->Text + LevelsLink + "&subregion=" +
					"&leftlon=" + LeftLon + "&rightlon=" + RightLon + "&toplat=" + TopLat + "&bottomlat=" + BottomLat +
					"&dir=%2Fgfs." + DownloadDate + DownloadTime;

//    Display1->Lines->Add(DownloadLink);

	SaveFileName = SavePath + SaveFileDataSet + ForecastLength->Text + ".grb";
    fs = new TFileStream(SaveFileName, fmCreate);
    try
		{
		Download1->Get(DownloadLink, fs);
		}
	catch(const EIdHTTPProtocolException &E)
		{
		//MessageDlg(E.Message, mtError, TMsgDlgButtons() << mbOK, 0);
		}




	Download1->Get(DownloadLink, fs);
	delete fs;
}
Display1->Text = Display1->Text + (" \n\r");

Display1->Lines->Add("All requests completed...");
ForecastLength->ItemIndex = dwnstart;

}


And Cancel button:
1
2
3
4
5
6
7
8
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Display1->Lines->Add(" ");
Display1->Lines->Add("Cancelled by user...");
Download1->Disconnect();

delete fs; /// THIS IS MY PROBLEM
}


Thank you.

Marian
Change delete fs; to delete fs; fs = 0;

Except Get doesn't know that fs has been deleted, we'll have to think a little harder.

But the bottom line is, unless the object that's managing the download supports cancellation and provides methods we can call, you can't support Cancel on the GUI.

What does Disconnect do?
Last edited on
It disconnects the socket...
I guess if you disconnect while downloading stuff, errors will be generated somewhere. The object doesn't appear to support cancellation, so if you want to support Cancel, you'll need to download with something that supports cancellation.
Before I find something good to replace Indy HTTP, I do it like, after clicking on Cancel, I get Canceled = true, and use it in the loop, wait untill the file is download, but then stop and do not continue in downloading the next file..... Not a solution, but it was important not to have locked files by previous process.
Topic archived. No new replies allowed.