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!
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.
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.
No, Dispose
properly frees strings and dynamic arrays in records, including nested ones. GetMem
/FreeMem(Data)
would create a memory leak, indeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With