Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 1 of 1 total
Thread Record Structures
Mon, Sep 11 2006 10:59 AMPermanent Link

Sean McCall
Hi,

Just wanted to see if anything has changed since D1 that might make it
safer to initialize records. I just spent quite a while tracking down a
memory leak in my application and a seemingly harmless function was the
cause:


type
  TRecord = record
    Serial: Integer; {a serial number}
    Text: String; {any dynamic string or array}
  end;
  ARecords: array of TRecord;

function RetrieveRecord(ASerial: Integer): TRecord
  {Return a cleared record if no match found.}
var
  I: Integer;
begin
  for I := 0 to High(ARecords) do begin
    Result := ARecords[I];
    if Result.Serial = ASerial then begin
      Exit;
    end;
  end; {for I}
  {
  Here is the memory leak. I've always initialized a record
  with a FillChar, but here the loop had already initialized Result
  to point to a valid record and the fillchar reset the dynamic string
  pointer to null without decrementing the strings reference counter.
  }
  FillChar(Result, SizeOf(Result), 0);
end;

My question:

Is there a more current way to clear a record that doesn't involve a
direct memory write using FillChar? It would be especially nice if the
compiler would generate a warning that the record may have already been
initialized when attempting to clear it again similar to the warning
that a variable may not have been initialized before it is used if you
access a variable that was not yet initialized.

Just curious,

Sean
Image