Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit call of ->DESTROY method considered harmful #3647

Closed
p5pRT opened this issue Mar 21, 2001 · 10 comments
Closed

Explicit call of ->DESTROY method considered harmful #3647

p5pRT opened this issue Mar 21, 2001 · 10 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 21, 2001

Migrated from rt.perl.org#6278 (status was 'rejected')

Searchable as RT6278$

@p5pRT
Copy link
Author

p5pRT commented Mar 21, 2001

From @mjdominus

  Explicitly calling DESTROY is also possible, but is usually never needed.

This seems misleading to me. When I teach Perl OOP classes, I always
emphasize that you should never call DESTROY manually. This is for
two reasons​:

1. In order to call DESTROY, you must have a reference to the object;
  so it must be alive. DESTROY assumes that the object is about to
  die, and may leave the objec tin an inconsistent state. You now
  have an object sitting around that thinks it has been destroyed,
  but it is still alive.

2. Later, when the object's reference count goes to zero, Perl will
  call DESTROY a *second* time. This may confuse the DESTROY
  function and generate incorrect results. To take a simple example,
  consider​:

  my $census;

  sub new {
  ...
  $census++;
  bless ...;
  }

  sub DESTROY {
  $census--;
  }

I am considering a doc patch that will say "Never call DESTROY manually."
Can anyone think of a reason why this might be bad advice?

Thanks.

2 similar comments
@p5pRT
Copy link
Author

p5pRT commented Mar 22, 2001

From @mjdominus

  Explicitly calling DESTROY is also possible, but is usually never needed.

This seems misleading to me. When I teach Perl OOP classes, I always
emphasize that you should never call DESTROY manually. This is for
two reasons​:

1. In order to call DESTROY, you must have a reference to the object;
  so it must be alive. DESTROY assumes that the object is about to
  die, and may leave the objec tin an inconsistent state. You now
  have an object sitting around that thinks it has been destroyed,
  but it is still alive.

2. Later, when the object's reference count goes to zero, Perl will
  call DESTROY a *second* time. This may confuse the DESTROY
  function and generate incorrect results. To take a simple example,
  consider​:

  my $census;

  sub new {
  ...
  $census++;
  bless ...;
  }

  sub DESTROY {
  $census--;
  }

I am considering a doc patch that will say "Never call DESTROY manually."
Can anyone think of a reason why this might be bad advice?

Thanks.

@p5pRT
Copy link
Author

p5pRT commented Mar 22, 2001

From @mjdominus

  Explicitly calling DESTROY is also possible, but is usually never needed.

This seems misleading to me. When I teach Perl OOP classes, I always
emphasize that you should never call DESTROY manually. This is for
two reasons​:

1. In order to call DESTROY, you must have a reference to the object;
  so it must be alive. DESTROY assumes that the object is about to
  die, and may leave the objec tin an inconsistent state. You now
  have an object sitting around that thinks it has been destroyed,
  but it is still alive.

2. Later, when the object's reference count goes to zero, Perl will
  call DESTROY a *second* time. This may confuse the DESTROY
  function and generate incorrect results. To take a simple example,
  consider​:

  my $census;

  sub new {
  ...
  $census++;
  bless ...;
  }

  sub DESTROY {
  $census--;
  }

I am considering a doc patch that will say "Never call DESTROY manually."
Can anyone think of a reason why this might be bad advice?

Thanks.

@p5pRT
Copy link
Author

p5pRT commented Jul 18, 2003

From @rspier

ISO8--%%- [ON] Wanderlust​: %p5p {T}(0/0/148)
(Summary)----L142--C0--Bot----
From​: Tony Bowden <tony@​kasei.com>
To​: perl5-porters@​perl.org
Cc​: mjd@​plover.com
Subject​: Bug #6278​: Explict call to DESTROY considered harmful
Date​: Fri, 18 Jul 2003 19​:15​:19 +0100

http​://bugs6.perl.org/rt2/Ticket/Display.html?id=6278

MJD considered changing perltoot to turn "Explicitly calling DESTROY is
also possible, but is usually never needed." into something more strongly
warning against ever doing it.

His original report never made it to p5p, but Damian's response did,
thus triggering a longish thread on what was wrong with perlbug.

However this left the bug report itself hanging.

As Damian pointed out, you may need to call DESTROY directly when
inheriting.

This leaves 2 main options​:

1) Leave everything as is, and close the bug.

2) Add something to the docs that changes the above line into something
akin to​:

Explicitly calling DESTROY should be avoided, other than when calling
SUPER​::DESTROY.

Thoughts/comments?

Tony

@p5pRT
Copy link
Author

p5pRT commented May 1, 2011

From @cpansprout

On Fri Jul 18 12​:44​:06 2003, robert wrote​:

ISO8--%%- [ON] Wanderlust​: %p5p {T}(0/0/148)
(Summary)----L142--C0--Bot----
From​: Tony Bowden <tony@​kasei.com>
To​: perl5-porters@​perl.org
Cc​: mjd@​plover.com
Subject​: Bug #6278​: Explict call to DESTROY considered harmful
Date​: Fri, 18 Jul 2003 19​:15​:19 +0100

http​://bugs6.perl.org/rt2/Ticket/Display.html?id=6278

MJD considered changing perltoot to turn "Explicitly calling DESTROY is
also possible, but is usually never needed." into something more strongly
warning against ever doing it.

His original report never made it to p5p, but Damian's response did,
thus triggering a longish thread on what was wrong with perlbug.

However this left the bug report itself hanging.

As Damian pointed out, you may need to call DESTROY directly when
inheriting.

This leaves 2 main options​:

1) Leave everything as is, and close the bug.

2) Add something to the docs that changes the above line into something
akin to​:

Explicitly calling DESTROY should be avoided, other than when calling
SUPER​::DESTROY.

Thoughts/comments?

Sometimes it’s necessary to call destructors pre-emptively in an END
block, if they rely on the presence of other objects. But in such cases,
one must ensure that the destructors are idempotent (e.g., return if
$self->{destroyed}++).

@p5pRT
Copy link
Author

p5pRT commented May 5, 2012

From @Hugmeir

On Sun May 01 15​:50​:43 2011, sprout wrote​:

On Fri Jul 18 12​:44​:06 2003, robert wrote​:

ISO8--%%- [ON] Wanderlust​: %p5p {T}(0/0/148)
(Summary)----L142--C0--Bot----
From​: Tony Bowden <tony@​kasei.com>
To​: perl5-porters@​perl.org
Cc​: mjd@​plover.com
Subject​: Bug #6278​: Explict call to DESTROY considered harmful
Date​: Fri, 18 Jul 2003 19​:15​:19 +0100

http​://bugs6.perl.org/rt2/Ticket/Display.html?id=6278

MJD considered changing perltoot to turn "Explicitly calling DESTROY is
also possible, but is usually never needed." into something more
strongly
warning against ever doing it.

His original report never made it to p5p, but Damian's response did,
thus triggering a longish thread on what was wrong with perlbug.

However this left the bug report itself hanging.

As Damian pointed out, you may need to call DESTROY directly when
inheriting.

This leaves 2 main options​:

1) Leave everything as is, and close the bug.

2) Add something to the docs that changes the above line into something
akin to​:

Explicitly calling DESTROY should be avoided, other than when calling
SUPER​::DESTROY.

Thoughts/comments?

Sometimes it’s necessary to call destructors pre-emptively in an END
block, if they rely on the presence of other objects. But in such cases,
one must ensure that the destructors are idempotent (e.g., return if
$self->{destroyed}++).

Well, perltoot doesn't exist anymore, and there's nothing about calling
DESTROY directly in perlobj and perlootut anymore, so maybe this ticket
can be marked as resolved. Though it would be nice if a executive
summary of this thread made it into perlobj.

@p5pRT
Copy link
Author

p5pRT commented May 7, 2012

From tchrist@perl.com

Well, perltoot doesn't exist anymore

Wanna bet? :)

--tom

@p5pRT
Copy link
Author

p5pRT commented Sep 19, 2013

From @jkeenan

On Sat May 05 05​:40​:11 2012, Hugmeir wrote​:

On Sun May 01 15​:50​:43 2011, sprout wrote​:

On Fri Jul 18 12​:44​:06 2003, robert wrote​:

ISO8--%%- [ON] Wanderlust​: %p5p {T}(0/0/148)
(Summary)----L142--C0--Bot----
From​: Tony Bowden <tony@​kasei.com>
To​: perl5-porters@​perl.org
Cc​: mjd@​plover.com
Subject​: Bug #6278​: Explict call to DESTROY considered harmful
Date​: Fri, 18 Jul 2003 19​:15​:19 +0100

http​://bugs6.perl.org/rt2/Ticket/Display.html?id=6278

MJD considered changing perltoot to turn "Explicitly calling
DESTROY is
also possible, but is usually never needed." into something more
strongly
warning against ever doing it.

His original report never made it to p5p, but Damian's response did,
thus triggering a longish thread on what was wrong with perlbug.

However this left the bug report itself hanging.

As Damian pointed out, you may need to call DESTROY directly when
inheriting.

This leaves 2 main options​:

1) Leave everything as is, and close the bug.

2) Add something to the docs that changes the above line into
something
akin to​:

Explicitly calling DESTROY should be avoided, other than when calling
SUPER​::DESTROY.

Thoughts/comments?

Sometimes it’s necessary to call destructors pre-emptively in an END
block, if they rely on the presence of other objects. But in such cases,
one must ensure that the destructors are idempotent (e.g., return if
$self->{destroyed}++).

Well, perltoot doesn't exist anymore, and there's nothing about calling
DESTROY directly in perlobj and perlootut anymore, so maybe this ticket
can be marked as resolved. Though it would be nice if a executive
summary of this thread made it into perlobj.

Hugmeir,

It appears to me that in pod/perlobj.pod, the section starting at
"=head2 Destructors" *does* talk about calling DESTROY directly.

Could you (or anyone review that section of perlobj.pod) and prepare a
patch if needed?

Otherwise, I recommend that this RT be closed in 30 days.

Thank you very much.
Jim Keenan

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2013

From @jkeenan

On Wed Sep 18 17​:03​:55 2013, jkeenan wrote​:

On Sat May 05 05​:40​:11 2012, Hugmeir wrote​:

On Sun May 01 15​:50​:43 2011, sprout wrote​:

On Fri Jul 18 12​:44​:06 2003, robert wrote​:

ISO8--%%- [ON] Wanderlust​: %p5p {T}(0/0/148)
(Summary)----L142--C0--Bot----
From​: Tony Bowden <tony@​kasei.com>
To​: perl5-porters@​perl.org
Cc​: mjd@​plover.com
Subject​: Bug #6278​: Explict call to DESTROY considered harmful
Date​: Fri, 18 Jul 2003 19​:15​:19 +0100

http​://bugs6.perl.org/rt2/Ticket/Display.html?id=6278

MJD considered changing perltoot to turn "Explicitly calling
DESTROY is
also possible, but is usually never needed." into something more
strongly
warning against ever doing it.

His original report never made it to p5p, but Damian's response
did,
thus triggering a longish thread on what was wrong with perlbug.

However this left the bug report itself hanging.

As Damian pointed out, you may need to call DESTROY directly when
inheriting.

This leaves 2 main options​:

1) Leave everything as is, and close the bug.

2) Add something to the docs that changes the above line into
something
akin to​:

Explicitly calling DESTROY should be avoided, other than when
calling
SUPER​::DESTROY.

Thoughts/comments?

Sometimes it’s necessary to call destructors pre-emptively in an END
block, if they rely on the presence of other objects. But in such
cases,
one must ensure that the destructors are idempotent (e.g., return if
$self->{destroyed}++).

Well, perltoot doesn't exist anymore, and there's nothing about calling
DESTROY directly in perlobj and perlootut anymore, so maybe this ticket
can be marked as resolved. Though it would be nice if a executive
summary of this thread made it into perlobj.

Hugmeir,

It appears to me that in pod/perlobj.pod, the section starting at
"=head2 Destructors" *does* talk about calling DESTROY directly.

Could you (or anyone review that section of perlobj.pod) and prepare a
patch if needed?

Otherwise, I recommend that this RT be closed in 30 days.

More than 30 days have elapsed; no patch submitted. Closing ticket.

Thank you very much.
Jim Keenan

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2013

@jkeenan - Status changed from 'open' to 'rejected'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant