Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I creating a memory leak here?

A very simple question:

type

TMyRecord = Record
  Int: Integer;
  Str: String;
end;

PMyRecord = ^TMyRecord;

var
  Data: PMyRecord;
begin
  New(Data);
  Data.Int := 42;
  Data.Str := 'Test';
  Dispose(Data);
end;

My question is, am I creating a memory leak here (with the String)? Should I call Data.Str := ''; before calling Dispose?

Thanks!

like image 789
Steve Avatar asked Nov 26 '10 14:11

Steve


People also ask

How do I know if I have a memory leak?

Running out of memory is the simplest way to identify a memory leak, and it's also the most common approach to uncovering one. That's also the most inconvenient way to find a leak. You'll probably notice your system slowing down before you run out of RAM and crash your application.

When can you tell that a memory leak will?

A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code (i.e. unreachable memory). A memory leak has symptoms similar to a number of other problems and generally can only be diagnosed by a programmer with access to the program's source code.


1 Answers

No, Dispose properly frees strings and dynamic arrays in records, including nested ones. GetMem/FreeMem(Data) would create a memory leak, indeed.

like image 151
himself Avatar answered Oct 23 '22 16:10

himself