Q&A

  • 흑흑... 파일이 열렸는데, 닫히지가 않네요...


안녕하세요...

어설픈 유틸을 하나 만들고 있는데요,

Memproof 로 메모리 체크를 했더니, 파일이 크리에이트는 됐는데,

close 된건 없네요...

어떻게 된건지... 제가 맨날 봐도... 해결책이 없네요...

살려주세요. ^^;



function TfrmMain.CheckFile(Path: String; rec: TSearchRec) : boolean;

var

Count, KeyLen: Integer;

I, FileHandle, X: Integer;

Buffer: array[0..31] of Char;



begin

if dlgSearchFiles <> nil then

lbFilePath.Caption := ExtractFileDir(Path);

lbFileDir.Caption := ExtractFileName(Path);



FileHandle := FileOpen(Path, fmOpenRead);

if FileHandle > 0 then begin

I := 0;

while aFileInfos[I].nSignaturePos >= 0 do begin

FileSeek(FileHandle, aFileInfos[I].nSignaturePos, 0);

Count := FileRead(FileHandle, Buffer, 32);

KeyLen := Length(aFileInfos[I].sSignature);

if (Count >= KeyLen) and (aFindingFiles[I] = True ) and

(CompareMem(@aFileInfos[I].sSignature[1], @Buffer, KeyLen)) and

(IsMatchedFile(FileHandle, aFileInfos[I].sSignature2) = true) then begin

AddFile(Path, rec, I);

Break;

end;

Inc(I);

end;

end

else;

Inc(ReadedFileSize, ((rec.Size + ClusterSize - 1) div ClusterSize) * ClusterSize);

if dlgSearchFiles <> nil then begin

X := Trunc(ReadedFileSize / AllocatedDiskSize * 100);

if X <= 100 then

begin

fuckyouPercent.Progress := X;

StatusBar.Panels[3].Text := '진행률 ' + IntToStr(X) + ' % 분석하였습니다.';

end

else

begin

// 진행율 100%

fuckyouPercent.Progress := 100;

StatusBar.Panels[3].Text := '완료하였습니다.';

btExit.Enabled := true;





end

end;

Result := True;

end;



// <---------------------------- 파일분석 끝. --------------------------------->



// 검색위치

function TfrmMain.IterateDir(Path: String) : boolean;

var

res: Word;

rec: TSearchRec;

begin

Result := True;

if Path[Length(Path)] <> '' then



Path := Path + '';



FillChar(rec, SizeOf(TSearchRec), 0);

res := FindFirst(Path + '*.*', faAnyFile - faVolumeId, rec);

while res = 0 do begin

if (rec.Attr and faDirectory) > 0 then begin

if (rec.Name <> '.') and (rec.Name <> '..') then

if IterateDir(Path + rec.Name) = False then begin

Result := False;

Break;

end;

end

else

begin

CheckFile(Path + rec.Name, rec);

end;



res := FindNext(rec);

Application.ProcessMessages;

if dlgSearchFiles = nil then begin

Result := False;

Break;

end;

end;

SysUtils.FindClose(rec);

end;



1  COMMENTS
  • Profile
    김영석 2000.12.20 03:17
    파일을 열었으면 파일을 닫아야 합니다.

    FileClose 함수를 쓰세요..



    소스에서 보면..



    FileHandle := FileOpen(Path, fmOpenRead);

    이부분에 try .. finally 를 써서 확실히 닫아줘야 합니다.



    FileHandle := FileOpen(Path, fmOpenRead);

    try

    { FileHandle 를 이용하는 부분.. }

    finally

    FileClose(FileHandle);

    end;







    박혜윤 wrote:

    >

    > 안녕하세요...

    > 어설픈 유틸을 하나 만들고 있는데요,

    > Memproof 로 메모리 체크를 했더니, 파일이 크리에이트는 됐는데,

    > close 된건 없네요...

    > 어떻게 된건지... 제가 맨날 봐도... 해결책이 없네요...

    > 살려주세요. ^^;

    >

    > function TfrmMain.CheckFile(Path: String; rec: TSearchRec) : boolean;

    > var

    > Count, KeyLen: Integer;

    > I, FileHandle, X: Integer;

    > Buffer: array[0..31] of Char;

    >

    > begin

    > if dlgSearchFiles <> nil then

    > lbFilePath.Caption := ExtractFileDir(Path);

    > lbFileDir.Caption := ExtractFileName(Path);

    >

    > FileHandle := FileOpen(Path, fmOpenRead);

    > if FileHandle > 0 then begin

    > I := 0;

    > while aFileInfos[I].nSignaturePos >= 0 do begin

    > FileSeek(FileHandle, aFileInfos[I].nSignaturePos, 0);

    > Count := FileRead(FileHandle, Buffer, 32);

    > KeyLen := Length(aFileInfos[I].sSignature);

    > if (Count >= KeyLen) and (aFindingFiles[I] = True ) and

    > (CompareMem(@aFileInfos[I].sSignature[1], @Buffer, KeyLen)) and

    > (IsMatchedFile(FileHandle, aFileInfos[I].sSignature2) = true) then begin

    > AddFile(Path, rec, I);

    > Break;

    > end;

    > Inc(I);

    > end;

    > end

    > else;

    > Inc(ReadedFileSize, ((rec.Size + ClusterSize - 1) div ClusterSize) * ClusterSize);

    > if dlgSearchFiles <> nil then begin

    > X := Trunc(ReadedFileSize / AllocatedDiskSize * 100);

    > if X <= 100 then

    > begin

    > fuckyouPercent.Progress := X;

    > StatusBar.Panels[3].Text := '진행률 ' + IntToStr(X) + ' % 분석하였습니다.';

    > end

    > else

    > begin

    > // 진행율 100%

    > fuckyouPercent.Progress := 100;

    > StatusBar.Panels[3].Text := '완료하였습니다.';

    > btExit.Enabled := true;

    >

    >

    > end

    > end;

    > Result := True;

    > end;

    >

    > // <---------------------------- 파일분석 끝. --------------------------------->

    >

    > // 검색위치

    > function TfrmMain.IterateDir(Path: String) : boolean;

    > var

    > res: Word;

    > rec: TSearchRec;

    > begin

    > Result := True;

    > if Path[Length(Path)] <> '' then

    >

    > Path := Path + '';

    >

    > FillChar(rec, SizeOf(TSearchRec), 0);

    > res := FindFirst(Path + '*.*', faAnyFile - faVolumeId, rec);

    > while res = 0 do begin

    > if (rec.Attr and faDirectory) > 0 then begin

    > if (rec.Name <> '.') and (rec.Name <> '..') then

    > if IterateDir(Path + rec.Name) = False then begin

    > Result := False;

    > Break;

    > end;

    > end

    > else

    > begin

    > CheckFile(Path + rec.Name, rec);

    > end;

    >

    > res := FindNext(rec);

    > Application.ProcessMessages;

    > if dlgSearchFiles = nil then begin

    > Result := False;

    > Break;

    > end;

    > end;

    > SysUtils.FindClose(rec);

    > end;

    >