greendot
asked on
pointer to objects as parameter of function
Hi,
Is it possible to pass an object of any type as a var parameter of a function?
I am wanting to wrap up the two ideas of:
Object.Free;
Object := nil;
I've tried a few incarnations of this, and nothing seems to work out.
I did the obvious:
function FreeObject(var Thing: TObject);
begin
...
Thing.Free;
Thing := nil;
end;
That fails... It only lets me pass pure TObjects as a parameter.
Then I tried:
function FreeObject(var Thing: Pointer);
begin
...
TObject(Thing^).Free;
Thing := nil;
end;
This fails with the same compiler warning as the first one...
Then I tried:
function FreeObject(var Thing);
begin
...
if Pointer(Thing) = nil then exit;
TObject(Thing).Free;
end;
This last one shows the best hope.. but no matter what I put in as a parameter for this, objects, records, integers, etc, it is always nil.. or that is what is reported to me. Maybe I don't know how to de-reference these correctly.
Is there some other way that I'm missing?
Thanks for your help.
-joe
Is it possible to pass an object of any type as a var parameter of a function?
I am wanting to wrap up the two ideas of:
Object.Free;
Object := nil;
I've tried a few incarnations of this, and nothing seems to work out.
I did the obvious:
function FreeObject(var Thing: TObject);
begin
...
Thing.Free;
Thing := nil;
end;
That fails... It only lets me pass pure TObjects as a parameter.
Then I tried:
function FreeObject(var Thing: Pointer);
begin
...
TObject(Thing^).Free;
Thing := nil;
end;
This fails with the same compiler warning as the first one...
Then I tried:
function FreeObject(var Thing);
begin
...
if Pointer(Thing) = nil then exit;
TObject(Thing).Free;
end;
This last one shows the best hope.. but no matter what I put in as a parameter for this, objects, records, integers, etc, it is always nil.. or that is what is reported to me. Maybe I don't know how to de-reference these correctly.
Is there some other way that I'm missing?
Thanks for your help.
-joe
Hi greendot
Greendot, your example of FreeObject comes close to the correct solution:
function FreeObject(var Thing: Pointer);
begin
TObject(Thing^).Free;
Thing := nil;
end;
The problem is you need to remove the ^
I.e. "TObject(Thing^).Free;" becomes "TObject(Thing).Free;"
Here is a routine I wrote for the purpose.
It has the following differences from inter's:
1. You can call FreeSafe for a nil pointer or a freed object.
2. You don't need to typecast when calling the procedure. You just pass in the address of the object. (See example below.) This makes coding that little bit faster. :)
procedure FreeSafe(AObj: Pointer);
begin
if (Assigned(AObj)) and (Assigned(Pointer(AObj^))) then begin
TObject(AObj^).Free;
Pointer(AObj^) := nil;
end;
end;
To call it:
FreeSafe(@MyObject);
It won't crash if you do the following:
FreeSafe(@MyObject);
FreeSafe(@MyObject);
JB
Greendot, your example of FreeObject comes close to the correct solution:
function FreeObject(var Thing: Pointer);
begin
TObject(Thing^).Free;
Thing := nil;
end;
The problem is you need to remove the ^
I.e. "TObject(Thing^).Free;" becomes "TObject(Thing).Free;"
Here is a routine I wrote for the purpose.
It has the following differences from inter's:
1. You can call FreeSafe for a nil pointer or a freed object.
2. You don't need to typecast when calling the procedure. You just pass in the address of the object. (See example below.) This makes coding that little bit faster. :)
procedure FreeSafe(AObj: Pointer);
begin
if (Assigned(AObj)) and (Assigned(Pointer(AObj^)))
TObject(AObj^).Free;
Pointer(AObj^) := nil;
end;
end;
To call it:
FreeSafe(@MyObject);
It won't crash if you do the following:
FreeSafe(@MyObject);
FreeSafe(@MyObject);
JB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
procedure FreeIt(var T :Pointer);
begin
TObject(T).Free;
T := nil;
end;
procedure TForm1.Button1Click(Sender
var
L : TList;
begin
L := TList.Create;
FreeIt(Pointer(L));
end;
Igor