Board index » delphi » Anonymous object creation: destructor automatically called?

Anonymous object creation: destructor automatically called?

I have been trying to find the answer to this, but the manuals that come
with Delphi don't go in-depth enough, and I couldn't find it in any of
my other books.

Say I have a class, TSomeClass which has a method SomeMethod which acts
on another TSomeClass and returns, oh, an Integer. If I do this

var
  I: Integer;

begin
  I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
end;

Have I just orphaned memory? That is, I anonymously created two
instances of TSomeClass, and since I didn't assign them to variables, I
can't destroy them. Does the compiler do the destroying for me? How does
it know which destructor to call? Does it just assume to call the
virtual destructor Destroy from TObject?

Which brings up another question: Does a variable declaration of a class
automatically call that class's constructor, like in C++?

Thanks,

Dave
da...@{*word*104}-fx.com

 

Re:Anonymous object creation: destructor automatically called?


Quote
Dave Shapiro wrote in message <3444E671.D567B...@{*word*104}-fx.com>...
>Say I have a class, TSomeClass which has a method SomeMethod which acts
>on another TSomeClass and returns, oh, an Integer. If I do this
>  I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
>Have I just orphaned memory? That is, I anonymously created two
>instances of TSomeClass, and since I didn't assign them to variables, I
>can't destroy them.

Yes, you're wasting two instances memory here.

Quote
>Does the compiler do the destroying for me? How does
>it know which destructor to call? Does it just assume to call the
>virtual destructor Destroy from TObject?
>Which brings up another question: Does a variable declaration of a class
>automatically call that class's constructor, like in C++?

He knows about destructor, but he never calls it automatically.
You're bound to do that yourself.

--
Alexander Petrosyan (PAF), Moscow.
email: p...@i-connect.ru; p...@chat.ru; p...@fbit.msk.su
page: http://www.i-connect.ru/~paf
FIDO: 2:5020/468.8
phone: (095)535-2222  12:00-21:00 MT

Re:Anonymous object creation: destructor automatically called?


Quote
Dave Shapiro wrote:

> I have been trying to find the answer to this, but the manuals that come
> with Delphi don't go in-depth enough, and I couldn't find it in any of
> my other books.

> Say I have a class, TSomeClass which has a method SomeMethod which acts
> on another TSomeClass and returns, oh, an Integer. If I do this

> var
>   I: Integer;

> begin
>   I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
> end;

> Have I just orphaned memory?

        Yes.

Quote
> That is, I anonymously created two
> instances of TSomeClass, and since I didn't assign them to variables, I
> can't destroy them. Does the compiler do the destroying for me?

        No.

Quote
> How does
> it know which destructor to call?

        Would be a good question.

Quote
> Does it just assume to call the
> virtual destructor Destroy from TObject?

        You can use anonymous objects in various ways sometimes, like

with AClass.Create do
begin
 SomeMethod;
 Free;
end;

Quote
> Which brings up another question: Does a variable declaration of a class
> automatically call that class's constructor, like in C++?

        No, referencing uncreated objects is a traditional "why doesn't
this work?" question.

--
David Ullrich

sig.txt not found

Re:Anonymous object creation: destructor automatically called?


In article <3444E671.D567B...@{*word*104}-fx.com>, Dave Shapiro

Quote
<da...@{*word*104}-fx.com> wrote:
>I have been trying to find the answer to this, but the manuals that come
>with Delphi don't go in-depth enough, and I couldn't find it in any of
>my other books.

>Say I have a class, TSomeClass which has a method SomeMethod which acts
>on another TSomeClass and returns, oh, an Integer. If I do this

>var
>  I: Integer;

>begin
>  I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
>end;

>Have I just orphaned memory? That is, I anonymously created two
>instances of TSomeClass, and since I didn't assign them to variables, I
>can't destroy them. Does the compiler do the destroying for me? How does
>it know which destructor to call? Does it just assume to call the
>virtual destructor Destroy from TObject?

>Which brings up another question: Does a variable declaration of a class
>automatically call that class's constructor, like in C++?

>Thanks,

>Dave
>da...@{*word*104}-fx.com

Constructors are never called automatically.  Unless you are using
COM interfaces in Delphi 3 the destructor will not be called in your
example above.  If you use COM interfaces the objects are reference
counted and will be released when no interfaces reference the object.

-----------------------------------------------------------------------------
Eric {*word*76}
ebl...@xmission.com

Re:Anonymous object creation: destructor automatically called?


Quote
Dave Shapiro wrote:
> Say I have a class, TSomeClass which has a method SomeMethod which acts
> on another TSomeClass and returns, oh, an Integer. If I do this

> var
>   I: Integer;

> begin
>   I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
> end;

> Have I just orphaned memory? That is, I anonymously created two
> instances of TSomeClass, and since I didn't assign them to variables, I
> can't destroy them. Does the compiler do the destroying for me? How does
> it know which destructor to call? Does it just assume to call the
> virtual destructor Destroy from TObject?

If you create it, you must free it. (The only exception is when you
create a control and then tell another control "Here, you own this" -
owned controls are freed whenever their owner is freed. Even here, it
can't hurt to free a control you create.)

There are a number of ways that you can do this.

(1) You can, of course, do something like "ThisInstance :=
TSomeClass.Create" and then, later, "ThisInstance.Free".

(2) You can use a "with" statement - "with TSomeClass.Create do begin
SomeMethod; Free; end" - though this won't work so well in this case,
where you want to also create an anonymous object and pass it as a
parameter.

(3) In D3, you can use interfaces. If TSomeClass implements IConsumer
and and IConsumable, you can rewrite your code as

Quote
>   I := (TSomeClass.Create as IConsumer).SomeMethod( TSomeClass.Create
>                                                     as IConsumable);

Interface tables are reference counted, just like huge strings. When you
pass the IConsumable reference to SomeMethod, it gets 'assigned' to the
formal parameter, thus setting the reference count to 1. When the
function returns, the formal parameter goes out of scope, the reference
count goes to 0, and the object is freed. Similarly for the IConsumer
objects 'assignment' to Self.

(4) Most egregiously, you could write TSomeClass.SomeMethod to both Free
Self and it's TSomeClass parameter. This is bad in at least two ways:
(A) objects are not being freed by the creating code. This makes the
code hard to follow and/or verify. While you can't avoid this in the
case of objects that are meant stick around for a while, these objects
will only last for the duration of the SomeMethod call. (B) If you (or
someone else) later call SomeMethod somewhere else, you may be nastily
surprised to find that it Free's Self and it's parameter ....

Quote
> Which brings up another question: Does a variable declaration of a class
> automatically call that class's constructor, like in C++?

No.

--

Personal Pages              http://www.midnightbeach.com/jon
Programming Publications    http://www.midnightbeach.com/jon/pubs
Homeschool Resource Page    http://www.midnightbeach.com/hs

Re:Anonymous object creation: destructor automatically called?


In article <34451F19.1...@math.okstate.edu>, ullr...@math.okstate.edu
says...

Quote
>    You can use anonymous objects in various ways sometimes, like

> with AClass.Create do
> begin
>  SomeMethod;
>  Free;
> end;

or  

with AClass.Create do
try
  SomeMethod;
finally
  Free;
end;

(small increment on a helpful and accurate answer)

--- Raoul

Re:Anonymous object creation: destructor automatically called?


Quote
Dave Shapiro <da...@{*word*104}-fx.com> wrote:
>I have been trying to find the answer to this, but the manuals that come
>with Delphi don't go in-depth enough, and I couldn't find it in any of
>my other books.
>  I := (TSomeClass.Create).SomeMethod(TSomeClass.Create);
>Have I just orphaned memory? That is, I anonymously created two
>instances of TSomeClass, and since I didn't assign them to variables, I
>can't destroy them.

Yes, you shouldn't do things like that. Still, it is a valid question
since this construction is often used, BUT with Tcomponent
descendants. TComponent descendants have an owner that keeps a
reference to it, and who frees it when it is destroyed. If you have
the sourcecode, take a look at the Tcomponent.create code.
In your case, you use a Tobject class, which destruction you must
manage yourself. Thus you need to keep a copy of each reference.
Good luck,

Antonie

(email address is of course without the spamthing)  

!! Remove @nospam from the return address !!
d950021@nos...@icpc00.icpc.fukui-u.ac.jp

Re:Anonymous object creation: destructor automatically called?


Just wanted to say thanks to everyone who responded. I got a fair amount
of email, and some posts on this group, all which agreed, and all which
told me exactly what I wanted to know.

Thanks again,

Dave

Other Threads