Write and memory leak

Could you help me solve this problem. Here is my code. I store (in this example) 10,000 lines, and when I try to delete them, only a certain amount of memory frees up the remaining leaks.

type
  PMyData = ^TMyData;
  TMyData = record
  Name:    string;
end;
////////

var
XList:Tlist;
//////////

// Here is how I add//
var
 MyData: PMyData;
 I:Integer;
begin
 for I:=0 to 10000 do begin
 New(MyData);
  MyData.Name:='Hello';
   XList.Add(TObject(MyData));
  end;
end;


///Here is how I delete///
var
 MyData: PMyData;
 I:Integer;
begin
 for I:= XList.Count - 1 downto 0 do begin
 MyData:=PMyData (XList[I]); /// I also used (XList.Items[I])  but the result is the same
  Dispose(MyData);
    XList.Delete(I);
end;
+3
source share
7 answers

I do not see a leak in this code. How do you determine if a leak is occurring? Are you looking at the task manager? If so, then this is not a reliable way to detect memory leaks. The VCL memory manager does not release the freed memory back to the OS; it is cached for later reuse. Task Manager displays the allocated memory. He has no idea how this memory is actually controlled by applications.

+6
source

, , .
, , - - , - (, ):

 ReportMemoryLeaksOnShutdown := True;

, - , , , .. .
.

, TaskManager , , -, , , , ; , .

, TaskManager ...

+5

Windows . FastMM4 .

+4

. , - , ? FastMM, , ?

, , . , , , Windows, . "", .

+3

Dispose (TObject)

(MyData ^);

0

,     , ... ...  ,   Finalize, , FreeMem Dispose.

0
source

Try:

FreeAndNil(XList.Items[I]); 
XList.Delete(I);
0
source

Source: https://habr.com/ru/post/1727543/


All Articles