🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.language.objectpascal
Article #32479

Re: Function that returns a Class

#32479
From: Guido Gybels
Date: Fri, 22 Nov 2002 13:42
62 lines
2902 bytes
"Dana Dill" <dana@danadill.com> wrote in message
news:3dde3048@newsgroups.borland.com...
> Actually I am not creating the object in the function.  The function
> accesses the object from a list (it is a lookup function) and returns the
> Object it finds.

Doesn't change the model. If you want to return a new object instance, you
can create one in the function and return that. You will still have to free
the object after you are done with it:

function DoSomething(AObject: TSomeClass): TSomeClass
begin
  ...
  Result:=TSomeClass.Create;
  Result.Assign(AObject);
  ...
end;

Then, somewhere else, you would have:

var
  Source, Destination: TSomeClass;
begin
  ...
  Source:=TSomeClass.Create;
  try
    ...
    Destination:=DoSomething(Source);
    try
      ...
    finally
      Destination.Free;
    end;
    ...
  finally
    Source.Free;
  end;
end;

Also, be careful with the assign. For instance, if your source object
contains references to other objects, the new object will contain references
to the same objects.

> My thinking was since I created a local object  I would have to free it as
> well locally, but of course when I do that it frees the object.

Please read my previous post again: the *reference* to your object is not
the same as the object instance itself. The local reference will go out of
scope, but that does not destroy the instance itself.
The rule is very clear: anything you create, you should destroy after you're
done with it. Interfaces automate some of the lifetime management through
reference counting, but for what you are describing they don't seem a proper
solution.

Guido


--
Guido Gybels
http://www.optimalcode.com/Guido
(Disclaimer: all remarks made apply to Intel processors unless specified
otherwise)
Technical Details
Message-ID: <3dde3459$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!diablo.netcom.net.uk!netcom.net.uk!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail
References: <3ddd61dd$1@newsgroups.borland.com> <3dde3048@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109d30,a90ac751e87011b2
X-Google-Attributes: gid109d30,public
X-Google-ArrivalTime: 2002-11-22 06:15:38 PST
From: "Guido Gybels" <guido.gybels@rnid.org.uk>
Newsgroups: borland.public.delphi.language.objectpascal
References: <3ddd61dd$1@newsgroups.borland.com> <3dde3048@newsgroups.borland.com>
Subject: Re: Function that returns a Class
Date: Fri, 22 Nov 2002 13:42:45 -0000
Lines: 64
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4920.2300
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4920.2300
NNTP-Posting-Host: 217.34.231.189
Message-ID: <3dde3459$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1037972569 217.34.231.189 (22 Nov 2002 05:42:49 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!diablo.netcom.net.uk!netcom.net.uk!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail