Solved

pointer to objects as parameter of function

Posted on 1998-02-25
3
323 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:greendot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1359801
What about typecasting friend?

procedure FreeIt(var T :Pointer);
begin
  TObject(T).Free;
  T := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  L : TList;
begin
  L := TList.Create;
  FreeIt(Pointer(L));
end;

Igor
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359802
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
0
 

Accepted Solution

by:
keyquotes earned 100 total points
ID: 1359803
Try this,

FreeObject(TList as TObject);

function FreeObject(var Thing: TObject);
begin
  ...
  Thing.Free;
  Thing := nil;
end;


0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month5 days, 6 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question