Article View: borland.public.delphi.language.objectpascal
Article #32518Re: Function that returns a Class
From: Guido Gybels
Date: Fri, 22 Nov 2002 08:29
Date: Fri, 22 Nov 2002 08:29
49 lines
2939 bytes
2939 bytes
"Dana Dill" <dana@danadill.com> wrote in message news:3ddd61dd$1@newsgroups.borland.com... > myVar := SomeFunc(Parm: Type): TObject; > > I want "myVar" to be a copy of the object and not a pointer. "myVar" is a > local var and when it gets freed it of course frees the assigned object as > well (Which is really the same. <g>) Variables that "contain" object instances are always pointers. They are implemented in current Delphi versions as 32-bit wide pointers to the actual object. Nothing prevents you from writing code that creates a new object and returns this as a result. In other words: MyObject:=TSomeClass.Create; will create an new instance of TSomeClass and MyObject will contain a pointer to that instance. If you create an object, you are responsible for freeing it after you're done with it. SO, if you do the following: var MyObject: TSomeClass; begin MyObject:=TSomeClass.Create; ... end; The instance of TSomeClass that you created never gets freed. The "MyObject" variable will go out of scope at the end of the procedure block, hence will be freed (it is actually implemented on the stackframe). So, the reference to the object you created is gone, but the object itself still exists (and since the only reference to it just went out of scope, you can't even free it anymore). By using Interfaces, rather than classic Delphi classes, however, you can leave some of the lifetime maintenance of your objects to be handled by the compiler (through reference counting, similar to how long strings are managed - after all, long string variables are mere pointers to the actual string as well). Interfaces come with an additional overhead, so use them only when appropriate. You'll find all relevant information in online help and a google search for some examples is usually helpful too. Guido -- Guido Gybels http://www.optimalcode.com/Guido (Disclaimer: all remarks made apply to Intel processors unless specified otherwise)
Technical Details
Message-ID:
<3dddeade$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.gamma.ru!Gamma.RU!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mailReferences:
<3ddd61dd$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109d30,a90ac751e87011b2,start X-Google-Attributes: gid109d30,public X-Google-ArrivalTime: 2002-11-22 01:16:42 PST From: "Guido Gybels" <guido.gybels@rnid.org.uk> Newsgroups: borland.public.delphi.language.objectpascal References: <3ddd61dd$1@newsgroups.borland.com> Subject: Re: Function that returns a Class Date: Fri, 22 Nov 2002 08:29:15 -0000 Lines: 51 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: <3dddeade$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1037953758 217.34.231.189 (22 Nov 2002 00:29:18 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.gamma.ru!Gamma.RU!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail