Given the following Delphi code, Foo is Free'd on FormClose, but TFoo.Destroy is not being called - and therefore Bar is not Free'd, leading to a memory leak?
Have I missed something here or shouldn't Foo.Free call Foo.Destroy at some point?
type
TBar = class
SomeInteger : integer;
end;
TFoo = class
Bar : TBar;
constructor Create();
destructor Destroy();
end;
var
Foo : TFoo;
implementation
constructor TFoo.Create;
begin
Bar := TBar.Create;
Bar.SomeInteger := 2;
end;
destructor TFoo.Destroy;
begin
Bar.Free;
Bar := nil;
showmessage('Destroyed!');
end;
procedure TForm10.FormCreate(Sender: TObject);
begin
Foo := TFoo.Create;
showmessage('Foo created');
end;
procedure TForm10.FormDestroy(Sender: TObject);
begin
Foo.Free;
Foo := nil;
end;
You must mark the signature of destructor with override.
destructor Destroy(); override;
And you should have inherited at the end of the destructor. But since your class is not derived from anything other than TObject I suspect that doesn't matter.
Destroy is virtual, and therefore you must override it in your descendant class.
TFoo = class
Bar : TBar;
constructor Create();
destructor Destroy(); override; // must add override here
end;
Without the override, your destructor is never called, and the base class one is instead.
Related
Good day,
I have class
TMn2Adapter = class(TPersistent)
private
FGrid: TStringGridPointer;
FList: TList<string>;
// ...
public
constructor Create(AGrid: TStringGridPointer);
destructor Destroy();
end;
constructor TMn2Adapter.Create(AGrid: TStringGridPointer);
begin
FGrid := AGrid;
FList := TList<string>.Create();
end;
destructor TMn2Adapter.Destroy;
begin
Dispose(FGrid);
FList.Free;
FList := nil;
inherited;
end;
and another one
TMn2Worker = class(TPersistent)
private
FMn2Adapter: TMn2Adapter;
public
constructor Create(AGrid: TStringGridPointer);
destructor Destroy();
end;
constructor TMn2Worker.Create(AGrid: TStringGridPointer);
begin
FMn2Adapter := TMn2Adapter.Create(AGrid);
end;
destructor TMn2Worker.Destroy;
begin
SysUtils.FreeAndNil(FMn2Adapter);
inherited;
end;
procedure TMn2Adapter.Parse;
begin
FList.Clear();
for I := 1 to FLenght do FList.Add((FGrid)^.Cells[2, I]);
end;
Creating TMn2Worker in a form mn2: TMn2Worker, doing some action and onFormClose I free this one mn2.Free. And I get report from Eurekalog about memory leak (see attached image). It seems to me that something strange happen in a parsing TStringGrid`s cell to the TList.
Your destructor is never called because you did not include the override directive. Declare destructors like this:
destructor Destroy; override;
Some of the rest of your code looks dubious, although we can't see enough to be say anything for sure. Unfortunately you removed most of the relevant code. In one of the variants you call FreeAndNil on a reference that you already set to nil. That clearly is somewhat pointless. And in the other you Dispose a pointer that you didn't allocate which smells odd. Finally, it's not obvious why you would have a pointer to a reference type. That usually indicates a misunderstanding of reference type variable assignment.
If I have two overloaded constructors, one without and one with parameters:
constructor Create; overload;
constructor Create(Param: TObject); overload;
If I want the code in the first one to run, does it make sense to call it within the second one? And inherited to call the parent constructor first as well?
constructor TMyType.Create(Param: TObject);
begin
inherited Create;
Create;
FParam := Param;
end;
Thanks!
If I want the code in the first one to run, does it make sense to call it within the second one And inherited to call the parent constructor first as well?
No. Because your 1st constructor should call inherited one itself, so in the end the inherited constructor would get called twice, which it most probably does not expect.
Otherwise, if your parameterless TMyType.Create() does not call inherited one, then it is hardly a proper constructor and should be just removed.
So the correct approach would be like that:
constructor TMyType.Create(Param: TObject); overload;
begin
Create();
FParam := Param;
end;
constructor TMyType.Create(); overload;
begin
inherited Create(); // for both constructors
...some common code
end;
However in Delphi there is yet another possibility.
constructor Create; overload;
constructor Create(Param: TObject); overload;
procedure AfterConstruction; override;
constructor TMyType.Create(Param: TObject);
begin
inherited Create();
FParam := Param;
end;
constructor TMyType.Create();
begin
inherited ;
... maybe some extra code
end;
procedure TMyType.AfterConstruction();
begin
inherited;
...some common code
end;
Note the difference though, when would "common code" be executed and when would do "FParam := Param;"
In the 1st way, the flow would be like
Create (Param)
..Create()
....Inherited Create()
....Common Code
..FParam := Param;
AfterConstruction (empty)
In the second the sequence would be different
Create(Param) or Create()
..Inherited Create()
..FParam := Param;
AfterConstruction
..Common Code
As you can see the order of those chunks being executed got reversed.
However maybe you don't need multiple constructors at all?
constructor TMyType.Create(const Param: TObject = nil);
begin
inherited;
... Some code
FParam := Param;
end;
Yes your code makes perfect sense and the constructor's calls do exactly what one should expect.
Delphi object model supports both constructors that call inherited constructors and constructors that do not call inherited ones.
If you are not sure try this:
program Project5;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TMyBase = class
constructor Create;
end;
TMyType = class(TMyBase)
constructor Create; overload;
constructor Create(Param: TObject); overload;
end;
constructor TMyBase.Create;
begin
Writeln('TMyBase.Create');
end;
constructor TMyType.Create;
begin
Writeln('TMyType.Create');
end;
constructor TMyType.Create(Param: TObject);
begin
inherited Create;
Create;
Writeln('TMyType.Create(Param)');
end;
begin
TMyType.Create(TObject.Create);
Readln;
end.
I'm trying to create an object structure where one object contains a list of other objects. I have a base class for both of these, similar to TCollection/TCollectionItem but a very custom implementation.
type
TMyItemBase = class;
TMyListBase = class;
TMyItemBaseClass = class of TMyItemBase;
TMyItemBase = class(TObject)
private
FOwner: TMyListBase;
public
constructor Create(AOwner: TMyListBase);
destructor Destroy; override;
property Owner: TMyListBase read FOwner;
end;
TMyListBase = class(TMyObjectBase)
private
FItems: TList;
FItemClass: TMyItemBaseClass;
function New: TMyItemBase;
function GetItem(Index: Integer): TMyItemBase;
public
constructor Create(AOwner: TMyMainObject; const ItemClass: TMyItemBaseClass);
destructor Destroy; override;
function Count: Integer;
procedure Clear;
property Items[Index: Integer]: TMyItemBase read GetItem; default;
end;
implementation
{ TMyItemBase }
constructor TMyItemBase.Create(AOwner: TMyListBase);
begin
FOwner:= AOwner;
end;
destructor TMyItemBase.Destroy;
begin
inherited;
end;
{ TMyListBase }
constructor TMyListBase.Create(AOwner: TMyMainObject; const ItemClass: TMyItemBaseClass);
begin
inherited Create(AOwner);
FItems:= TList.Create;
FItemClass:= ItemClass;
end;
destructor TMyListBase.Destroy;
begin
Clear;
FItems.Free;
inherited;
end;
procedure TMyListBase.Clear;
begin
while FItems.Count > 0 do begin
TMyItemBase(FItems[0]).Free;
FItems.Delete(0);
end;
end;
function TMyListBase.Count: Integer;
begin
Result:= FItems.Count;
end;
function TMyListBase.GetItem(Index: Integer): TMyItemBase;
begin
Result:= TMyItemBase(FItems[Index]);
end;
function TMyListBase.New: TMyItemBase;
begin
Result:= FItemClass.Create(Self);
FItems.Add(Result);
end;
(Pseudo code, sorry if there are any typos)
Problem is, when a new item is created (via TMyListBase.New), the object is successfully created, but the inheritance and all its fields are not (The inherited object's constructor is never even called)...
type
TMyItem = class;
TMyItems = class;
TMyItem = class(TMyItemBase)
private
//various unrelated fields
public
constructor Create;
destructor Destroy; override;
//various unrelated properties
end;
TMyItems = class(TMyListBase)
private
function GetItem(Index: Integer): TMyItem;
function New: TMyItem;
public
constructor Create(AOwner: TMyMainObject);
destructor Destroy; override;
property Items[Index: Integer]: TMyItem read GetItem; default;
end;
implementation
{ TMyItem }
constructor TMyItem.Create;
begin
inherited;
//Initialize some fields
end;
destructor TMyItem.Destroy;
begin
//Destroy some fields
inherited;
end;
{ TMyItems }
constructor TMyItems.Create(AOwner: TMyMainObject);
begin
inherited Create(AOwner, TMyItem);
end;
destructor TMyItems.Destroy;
begin
inherited;
end;
function TMyItems.GetItem(Index: Integer): TMyItem;
begin
Result:= TMyItem(inherited Channels[Index]);
end;
function TMyItems.New: TMyItem;
begin
Result:= TMyItem(inherited New);
end;
It appears to be something wrong with the New function, but I cannot figure it out. Even though I'm creating the item as its intended item class, it gets further treated as if it were the base class, and none of the inherited members are accessible (giving access violations) because the inherited constructor is never called.
What am I overlooking / doing wrong here?
Your TMyItemBase.Create() constructor needs to be declared as virtual, and then descendant classes need to override it. This is important when constructing objects using metaclass types. For example:
type
TMyItemBase = class(TObject)
...
public
constructor Create(AOwner: TMyListBase); virtual;
...
end;
constructor TMyItemBase.Create(AOwner: TMyListBase);
begin
inherited Create;
FOwner := AOwner;
end;
type
TMyItem = class(TMyItemBase)
...
public
constructor Create(AOwner: TMyListBase); override;
...
end;
constructor TMyItem.Create(AOwner: TMyListBase);
begin
inherited Create(AOwner);
...
end;
Try to use generics!
type
TMyItem = class
//Implement your class here
end;
//Use it now!
var
MyItemsList:TObjectList<TMyItem>;
// or implement your generic class customization:
type
TVeryMyItemsList = class(TObjectList<TMyItem>)
end;
// Or implement your generic class
type
TMyGeneric<T> = class(TObjectList<T>)
end;
//where T is any type
I'm hoping to create something like a "TOwnedStringList" (class name is a fiction) that I could construct as:
sl := TOwnedStringList.Create(Self);
sl.Sorted := True;
sl.Duplicates := dupIgnore;
sl.Add(...);
// etc...
Where Self could be a Form (for example), so that the Owner will auto free the StringList.
I want to be able to avoid calling sl.Free myself.
Is This possible?
That's going to be a little messy. You'd need to do something like this.
type
TOwnerComponent = class(TComponent)
private
FOwnedObject: TObject;
public
constructor Create(Owner: TComponent; OwnedObject: TObject);
destructor Destroy; override;
end;
TOwnedStringList = class(TStringList)
private
FOwner: TOwnerComponent;
public
constructor Create(Owner: TComponent);
destructor Destroy; override;
end;
{ TOwnerComponent }
constructor TOwnerComponent.Create(Owner: TComponent; OwnedObject: TObject);
begin
inherited Create(Owner);
FOwnedObject := OwnedObject;
end;
destructor TOwnerComponent.Destroy;
begin
FOwnedObject.Free;
inherited;
end;
{ TOwnedStringList }
constructor TOwnedStringList.Create(Owner: TComponent);
begin
inherited Create;
if Assigned(Owner) then
FOwner := TOwnerComponent.Create(Owner, Self);
end;
destructor TOwnedStringList.Destroy;
begin
if Assigned(FOwner) and not (csDestroying in FOwner.ComponentState) then
begin
FOwner.FOwnedObject := nil;
FOwner.Free;
end;
inherited;
end;
Basically you create an instance of TOwnerComponent that is owned by the Owner that you pass to TOwnedStringList.Create. When that Owner dies, it destroys the TOwnerComponent which in turn destroys your string list.
The code is resilient to an explicit Free being called on the string list.
Why in the code below, do I get the "Failed" message rather than "Succeeded"
Background: I like to have class procedures that instantiate their owner object, do something, and then free it.
However, this approach doesn't work if I have a descendant object:
Any suggestions on how to provide class procedures in a base class that can be called as a child? Am I thinking about this wrongly?
Type
TBase = class(TObject)
Protected
Procedure Proc1; Virtual;
Public
Class Procedure MyClassProc;
end;
Class Procedure TBase.MyClassProc;
Var
Base: TBase;
begin
Base := TBase.Create;
Base.Proc1;
Base.Free;
end;
Procedure TBase.Proc1;
begin
Assert(FALSE, 'Failed');
end;
type
TChild = class(TBase)
protected
Procedure Proc1; Override;
end;
Procedure TChild.Proc1;
begin
ShowMessage('Succeeded');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TChild.MyClassProc;
end;
You can do it easily with meta-programmation! Just change "TBase.Create" to "Self.Create" "self" represents the current class, it doesn't metter if is a base o a child class.
Type
TBase = class(TObject)
Protected
Procedure Proc1; Virtual;
Public
Class Procedure MyClassProc;
end;
Class Procedure TBase.MyClassProc;
Var
MyObject: TBase;
begin
// MyObject := TBase.Create;
MyObject := Self.Create; // The Magic goes here, self is the class that's calling this method, in this case, TChild }
MyObject.Proc1;
MyObject.Free;
end;
Procedure TBase.Proc1;
begin
Assert(FALSE, 'Failed');
end;
type
TChild = class(TBase)
protected
Procedure Proc1; Override;
end;
Procedure TChild.Proc1;
begin
ShowMessage('Succeeded');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TChild.MyClassProc;
end;
Strip everything down to the bare minimum, and you will see that you only ever create a TBase instance, so consequently only TBase.Proc1() will ever be called. If you want to have TChild.Proc1() be called you need to create a TChild instance and let polymorphism work its magic.
There could however be better ways to achieve your goal (whatever it is) than to have a class method create an object instance to do something. Maybe you should clarify your question.
Here it is
Add
TBase = class;
TBaseClass = class of TBase;
TBase = class(TObject)
protected
class function GetBaseClass: TBaseClass; virtual;
function TBase.GetBaseClass: TBaseClass;
begin
Result := TBase;
end;
TChild = class(TBase)
protected
class function GetBaseClass: TBaseClass; override;
function TChild.GetBaseClass: TBaseClass;
begin
Result := TChild;
end;
Change
from
Base := TBase.Create;
to
Base := GetBaseClass.Create;
Enjoy your work
Cheer
A Pham