Answer the question
In order to leave comments, you need to log in
Clearing the memory occupied by class properties on form close?
Greetings.
I'm studying Delphi and came across such a problem: you need to clear the memory occupied by the properties of a class instance when the form is closed.
The implementation is as follows (names, of course, fictitious):
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ExtDlgs, ComCtrls;
type
TForm1 = class(TForm)
...
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
end;
MyClass = class
St_L1, St_L2: TStringList;
constructor Create();
end;
MainClass = class
MyStrings: MyClass;
...
procedure CreateMyClass();
end;
var
MC: MainClass;
implementation
{$R *.dfm}
constructor MyClass.Create();
begin
St_L1 := TStringList.Create;
St_L2 := TStringList.Create;
end;
destructor MyClass.Destroy();
begin
St_L2 .Free;
St_L2 .Free;
end;
procedure MainClass.CreateMyClass();
begin
MyStrings := MyClass.Create();
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MC.CreateMyClass();
end;
// ...
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MC.Destroy();
end;
end.
type
Books = class
Title: String;
Author: String;
PublYear: Integer;
constructor Create(Title: String; Author: String; PublYear: Integer);
end;
Library = class
Book: array [1..100] of Books;
Counter;
constructor Create();
procedure AddBook(T: String; A: String; PY: Integer);
function GetBookTitle(N: Integer): String;
end;
constructor Create(Title: String; Author: String; PublYear: Integer);
begin
self.Title := Title;
self.Author := Author;
self.PublYear := PublYear;
end;
constructor Library.Create();
begin
Counter := 0;
end;
procedure Library.AddBook(T: String; A: String; PY: Integer);
begin
Counter := Counter + 1;
Book[Counter] := Books.Create(T, A, PY);
end;
function GetBookTitle(N: Integer): String;
begin
Result := Book[N].Title;
end;
var
LB: Library;
begin
LB := Library.Create();
LB.AddBook('Утиные истории', 'Дональд Дак', 2999);
ShowMessage(LB.GetBookTitle(1));
end.
Answer the question
In order to leave comments, you need to log in
1. MS - not initialized.
That is, it must appear somewhere else:
Otherwise, the behavior of the program is not predictable.
2. There will be no auto-deletion here. Unless you explicitly delete TStringLists, they will remain in memory (which is logical, because, for example, you could pass them to other objects). It follows that FList of type TStringItemList (which is essentially a dynamic array) and all the strings that are stored in them will remain in memory forever.
3. If you are wondering if your program has memory leaks, use
reportmemoryleaksonshutdown := true;
Google will tell you the details.
4. About the implementation with the global class - I didn't quite understand the technique, but it looks very controversial.
5. MC.Destroy - it's better to replace it with FreeAndNil(MC), but still, you need to write
TMainClass = class(TObject)
...
destructor Destroy; override;
end;
...
destructor TMainClass.Destroy;
begin
MyStrings.Free;
end;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question