Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writeln causing memory leak 1

Status
Not open for further replies.

mattlacey

Programmer
Oct 11, 2001
22
GB
I have a program (Written in Delphi6) that acts as a mesage server for lots of different serial communications.
It receives input from different serial devices and then sends messages back, writes to a database and/or passes the message(data from the serial device) to another machine.

When a message is processed I record details of the message and what was done with it in a log file. This writing to a log file is causing memory usage to slowly creep up.

The code I am using to create the log is as below (simplified for posting here):
Code:
const
  LOGDIR : String = 'C:\Logs\';

procedure TfrmMain.Write2Log(sSerialData : String);
var
  TF: TextFile;
  sFileName: String;
  sLine: String;

begin
  sFileName := FormatDateTime('yyyymmdd', Now) + '.log';

  AssignFile(TF, LOGDIR + sFileName);

  if not FileExists(LOGDIR + sFileName) then
    Rewrite(TF) 
  else
    Append(TF);

  sLine := FormatDateTime('hh:mm:ss:zzz', Now) + '  ' + sSerialData;

  Writeln(TF, sLine) ;

  Flush(TF);

  CloseFile(TF);

end;

It all works fine, the log is created and appended to OK but memory usage, slowly but consistently, creeps up. If I comment out the Writeln procedure then there is no change in memory usage.

I have run both MemProof and MemorySleuth and neither can find a problem.

There is obviously nothing wrong with the Writeln procedure as I use it all the time and have never come across this problem. I've even copied chunks of this code into a new app, and it runs fine there.

Does anybody have any ideas what the problem could be.

The variables FT & sLine are unique to this procedure so it can't be anything they're doing.

Any suggestions would be greatly received.

Thanks

Matt.
 
I suspect you are using Windows to measure memory usage. If Memproof says it's ok I wouldn't worry about Windows saying it's using more.
 
I am worried about it because the app has to run 24/7 and if it's allocating memory and not freeing it, it will eventually run out, as it typically does after a few hours of intensive usage.
 
Total Guess, but is it possible that AssignFile is allocating space for the filename and that CloseFile only removes the association and does not free the space allocated for the name variable?.
Is there some way you can keep the Log file open?
Steve.
 
Hi

I also have a program that runs 24\7 and which maintains a log of what it does
I use stream to write to a file instead of your method .
try if this function helps with the memory usage problem.

str:string that you want to write
strfile: log file name
boonew : false if you dont want the file to be overwritten evrytime this function is called.

function Tform1.SaveStringInFile(str,strFile:string;booNew:boolean):boolean;
Var
booExists:boolean;
intSize,ix:integer;
Buffer:pChar;
OutFile:TFileStream;
begin
booExists:=FileExists(strFile);
if (booExists) and (not booNew) then
begin
OutFile:=TFileStream.Create(strFile,fmOpenWrite);
end
else
begin
OutFile:=TFileStream.Create(strFile,fmCreate);
end;
OutFile.Position:=OutFile.Size;
//
intSize:=Length(str);
GetMem(Buffer,intSize+10);
FillChar(Buffer^,intSize+10,0);
for ix:=0 to intSize-1 do
begin
(Buffer+ix)^:=str[ix+1];
end;
//
OutFile.Write(Buffer^,intSize);
OutFile.Free;
FreeMem(Buffer);
Result:=true;
end;

 
earlrainer, You are my new programming hero!

Your stream is working marvelously.

Be sure this function will be included in many of my programs in the future.

Thankyou very much.

Matt.
 
I've also experienced similar problems. EarlRainer's approach works very well because it uses a buffer and does not do any string allocation in the subroutine.

I think that you will find that your creeping memory problem would also go away if you put

SLine:='';
SFileName:='';

at the end of your subroutine. I suspect that the problem is actually a Delphi bug. Delphi does not appear to clean up dynamically allocated local strings properly. I have seen some writeups on this problem but don't remember where.

Roger Fedyk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top