Board index » delphi » Anybody know what can close a file?

Anybody know what can close a file?

Hey everyone, the project is going great, but I've just found a
new and rather large bug. When I try to merge sort over 2000
records, my TP program crashes complaining that a file is close,
however, when I look at the code, at no point is the close(X)
command invoked before a file is finished with.

I had a problem like this the other day, which was solved by a
rewrite, but I don't particullarly want to rewrite this code.

I was wondering if anyone has any idea of what it is I should be
looking out for.

Thanks in advance

Jim

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

 

Re:Anybody know what can close a file?


Jim <james_darleyNOjaS...@hotmail.com.invalid> said
Quote
>Hey everyone, the project is going great, but I've just found a
>new and rather large bug. When I try to merge sort over 2000
>records, my TP program crashes complaining that a file is close,
>however, when I look at the code, at no point is the close(X)
>command invoked before a file is finished with.

>I had a problem like this the other day, which was solved by a
>rewrite, but I don't particullarly want to rewrite this code.

>I was wondering if anyone has any idea of what it is I should be
>looking out for.

At a guess, I'd say you probably have IO checking off {$I-} and are
not checking for any IO errors in IOresult. If you get an error then
nothing will happen until you turn IO checking back on and I'll further
guess you then try doing something to the file after switching the
error checking back on.
--
Pedt Scragg     http://signpost-design.co.uk/   ODP Editor: http://dmoz.org/
Signpost Web Design, Wrecsam, North Wales

Re:Anybody know what can close a file?


No, that can't be right, I only use file error checking once,
and I definately turn it back on before I exit the procedure it
is used on.

The bug only seems to occur when the procedure has been run a
number of times, here is the procedure that is causing the
bother:

Procedure MergeFiles(Path, IndexFile1, IndexFile2, Field:String);
Var Index1, Index2, Target:File;
    IndexBuffer1, IndexBuffer2:IndexArray;
    TargetBuffer:Array [1..(2*MaxSortSize)] of IndexRec;
    IndexLastRead1, IndexLastRead2:Word;
    IndexPointer1, IndexPointer2, TargetPointer, i, FS1,
FS2:Integer;
    Exit1, Exit2:Boolean;
Begin
     Assign(Index1, Path+IndexFile1);
     Assign(Index2, Path+IndexFile2);
     Assign(Target, TempPath+'TEMP.MRG');

     Reset(Index1, SizeOf(IndexRec));
     Reset(Index2, SizeOf(IndexRec));
     Rewrite(Target, SizeOf(IndexRec));

     FS1:=FileSize(Index1);
     FS2:=FileSize(Index2);

     BlockRead(Index1, IndexBuffer1, MaxSortSize,
IndexLastRead1);
     If IndexLastRead1>0 then DelDeadIndexRecords(IndexBuffer1,
IndexLastRead1);
     BlockRead(Index2, IndexBuffer2, MaxSortSize,
IndexLastRead2);
     If IndexLastRead2>0 then DelDeadIndexRecords(IndexBuffer2,
IndexLastRead2);

     IndexPointer1:=1;
     IndexPointer2:=1;
     TargetPointer:=1;

     If (FS1<>0) and (FS2<>0) then
     Begin
     Repeat
           If ((Field='Text') and (IndexBuffer1
[IndexPointer1].Text>IndexBuffer2[IndexPointer2].Text)) or
              ((Field='MainReference') and
              (IndexBuffer1
[IndexPointer1].MainReference>IndexBuffer2
[IndexPointer2].MainReference)) then
           Begin
                TargetBuffer[TargetPointer]:=IndexBuffer2
[IndexPointer2];
                Inc(TargetPointer);
                Inc(IndexPointer2);
           End;

           If ((Field='Text') and (IndexBuffer1
[IndexPointer1].Text<=IndexBuffer2[IndexPointer2].Text)) or
              ((Field='MainReference') and
              (IndexBuffer1
[IndexPointer1].MainReference<=IndexBuffer2
[IndexPointer2].MainReference)) then
           Begin
                TargetBuffer[TargetPointer]:=IndexBuffer1
[IndexPointer1];
                Inc(TargetPointer);
                Inc(IndexPointer1);
           End;

           If (IndexPointer1>IndexLastRead1) and (not eof
(Index1)) then
              Begin
                   BlockRead(Index1, IndexBuffer1, MaxSortSize,
IndexLastRead1);
                   DelDeadIndexRecords(IndexBuffer1,
IndexLastRead1);
                   IndexPointer1:=1;
              End;

           If (IndexPointer2>IndexLastRead2) and (not eof
(Index2)) then
              Begin
                   BlockRead(Index2, IndexBuffer2, MaxSortSize,
IndexLastRead2);
                   DelDeadIndexRecords(IndexBuffer2,
IndexLastRead2);
                   IndexPointer2:=1;
              End;

           If TargetPointer=(2*MaxSortSize)+1 then
              Begin
                   BlockWrite(Target, TargetBuffer,
2*MaxSortSize);
                   TargetPointer:=1;
              End;

     Until ((eof(Index1)) and (IndexPointer1>IndexLastRead1)) or
           ((eof(Index2)) and (IndexPointer2>IndexLastRead2));

*** The error occurs on the line above, saying that Index2 is
not open, yet it is opened at the beginning ***

     BlockWrite(Target, TargetBuffer, TargetPointer-1);

     If IndexPointer1>IndexLastRead1 then
     Begin
          For i:=IndexPointer2 to IndexLastRead2 do Blockwrite
(Target, IndexBuffer2[i], 1);
          Repeat
                BlockRead(Index2, IndexBuffer2, MaxSortSize,
IndexLastRead2);
                DelDeadIndexRecords(IndexBuffer2,
IndexLastRead2);
                If IndexLastRead2>0 then BlockWrite(Target,
IndexBuffer2, IndexLastRead2);
          Until (eof(Index2)) and (IndexLastRead2=0);
     End
     else
     Begin
          For i:=IndexPointer1 to IndexLastRead1 do BlockWrite
(Target, IndexBuffer1[i], 1);
          Repeat
                BlockRead(Index1, IndexBuffer1, MaxSortSize,
IndexLastRead1);
                DelDeadIndexRecords(IndexBuffer1,
IndexLastRead1);
                If IndexLastRead1>0 then BlockWrite(Target,
IndexBuffer1, IndexLastRead1);
          Until (eof(Index1)) and (IndexLastRead1=0);
     End;
     End
     else
     If (FS1=0) XOR (FS2=0) then
        If FS1=0 then
           Begin
                BlockWrite(Target, IndexBuffer2, IndexLastRead2);
                Repeat
                      BlockRead(Index2, IndexBuffer2,
MaxSortSize, IndexLastRead2);
                      DelDeadIndexRecords(IndexBuffer2,
IndexLastRead2);
                      BlockWrite(Target, IndexBuffer2,
IndexLastRead2);

                Until (eof(Index2)) and (IndexLastRead2=0);
           End
           else
           Begin
                BlockWrite(Target, IndexBuffer1, IndexLastRead1);
                Repeat
                      BlockRead(Index1, IndexBuffer1,
MaxSortSize, IndexLastRead1);
                      DelDeadIndexRecords(IndexBuffer1,
IndexLastRead1);
                      BlockWrite(Target, IndexBuffer1,
IndexLastRead1);
           Until (eof(Index1)) and (IndexLastRead1=0);
           End;

     Close(Index1);
     Close(Index2);
     Erase(Index1);
     Erase(Index2);
     Close(Target);
     Rename(Target, Path+IndexFile1);
End;

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Anybody know what can close a file?


In article <02cc53ed.f66ef...@usw-ex0106-045.remarq.com>,

Quote
Jim  <james_darleyNOjaS...@hotmail.com.invalid> wrote:
>No, that can't be right, I only use file error checking once,
>and I definately turn it back on before I exit the procedure it
>is used on.

>The bug only seems to occur when the procedure has been run a
>number of times, here is the procedure that is causing the
>bother:

>Procedure MergeFiles(Path, IndexFile1, IndexFile2, Field:String);
>Var Index1, Index2, Target:File;
>    IndexBuffer1, IndexBuffer2:IndexArray;
>    TargetBuffer:Array [1..(2*MaxSortSize)] of IndexRec;
>    IndexLastRead1, IndexLastRead2:Word;
>    IndexPointer1, IndexPointer2, TargetPointer, i, FS1,
>FS2:Integer;
>    Exit1, Exit2:Boolean;
>Begin
>     Assign(Index1, Path+IndexFile1);
>     Assign(Index2, Path+IndexFile2);
>     Assign(Target, TempPath+'TEMP.MRG');

>     Reset(Index1, SizeOf(IndexRec));
>     Reset(Index2, SizeOf(IndexRec));
>     Rewrite(Target, SizeOf(IndexRec));

>     FS1:=FileSize(Index1);
>     FS2:=FileSize(Index2);

>     BlockRead(Index1, IndexBuffer1, MaxSortSize,
>IndexLastRead1);

How is the indexArray defined???? Are you sure you do not read past the
buffer?  Reading past buffers can cause just errors like that.

You should give all relevant type, variable and constant definitions.

Osmo

Re:Anybody know what can close a file?


MaxSortSize is a constant, usually set to 50

IndexArray is an array [1..MaxSortSize] of IndexRecs

IndexRec is defined as follows

IndexRec=Record
Text:String[14];
Position, Reference, MainReference:Longint;
Enabled:Boolean;
End;

IndexPointer1 and IndexPointer2 refer to the next records in
IndexBuffer1 and IndexBuffer2. TargetPointer refers to the next
Position in TargetBuffer.

IndexLastRead1 and IndexLastRead2 return the number of records
read from Index1 and Index2 after Disabled records are moved
from the array.

I'll look at the code and see if I am reading past the end of
the array. Thanks for the suggestion

James

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Anybody know what can close a file?


Thanks Osmo,

I found the bug sitting in the lines

If TargetPointer=(2*MaxSortSize)+1 then
Begin
BlockWrite(Target, TargetBuffer, 2*MaxSortSize);
TargetPointer:=1;
End;

For some unknown reason it wasn't entering this section of code
when TargetPointer was 101 (which is the (2*MaxSortSize)+1).

This problem was driving me mad!

I owes you a beer

Thanks Again

James

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Re:Anybody know what can close a file?


JRS:  In article <03417c42.3bfc5...@usw-ex0103-023.remarq.com> of Mon,
17 Apr 2000 06:21:57 seen in news:comp.lang.pascal.borland, Jim

Quote
<james_darleyNOjaS...@hotmail.com.invalid> wrote:

>I'll look at the code and see if I am reading past the end of
>the array. Thanks for the suggestion

You should have all run-time checks ON while debugging - it saves
everybody a lot of time.  Alt-O, C; R S I c & D L y to be selected.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Other Threads