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

Destruction happens too late when using an INIT block #15028

Open
p5pRT opened this issue Nov 3, 2015 · 5 comments
Open

Destruction happens too late when using an INIT block #15028

p5pRT opened this issue Nov 3, 2015 · 5 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 3, 2015

Migrated from rt.perl.org#126556 (status was 'open')

Searchable as RT126556$

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2015

From @atoomic

This clearly looks like a bug to me, but this might be a limitation I'm not aware of, as I'm able to reproduce the same issue with perl 5.14.4 or perl 5.22.0

Running this program shows that $x destruction is not happening when expecting,

Note that removing the INIT block or using an eval quote inside the INIT block avoid the issue
and does not increase the REFCNT.

# test.pl ____________
#!./perl

use feature q/say/;
use B ();

my $ok;

sub X​::DESTROY {
  say 'Destroyed @​'. ${^GLOBAL_PHASE};
  $ok = 1;
}

{
  my $x;
  BEGIN { $x = 42 }
  # no problem when using eval quote or removing the INIT block
  # INIT { eval q/$x = 43/ }
  INIT { $x = 43 }
  $x = bless {}, "X";
  say "x REFCNT​: ", B​::svref_2object( \$x )->REFCNT;
  undef($x) if grep { /force/ } @​ARGV;
}

say "x REFCNT​: ", eval { B​::svref_2object( \$x )->REFCNT };

say $ok ? "ok" : "not ok";

# ________________________________________

perl test.pl
x REFCNT​: 3
x REFCNT​: 2
not ok
Destroyed @​RUN
perl test.pl --force
x REFCNT​: 3
Destroyed @​RUN
x REFCNT​: 2
ok

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2015

From @atoomic

test.pl

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2015

From @ysth

On Tue, Nov 3, 2015 at 9​:33 AM, Atoomic <perlbug-followup@​perl.org> wrote​:

# New Ticket Created by Atoomic
# Please include the string​: [perl #126556]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=126556 >

This clearly looks like a bug to me, but this might be a limitation I'm not aware of, as I'm able to reproduce the same issue with perl 5.14.4 or perl 5.22.0

Running this program shows that $x destruction is not happening when expecting,

Note that removing the INIT block or using an eval quote inside the INIT block avoid the issue
and does not increase the REFCNT.

# test.pl ____________
#!./perl

use feature q/say/;
use B ();

my $ok;

sub X​::DESTROY {
say 'Destroyed @​'. ${^GLOBAL_PHASE};
$ok = 1;
}

{
my $x;
BEGIN { $x = 42 }
# no problem when using eval quote or removing the INIT block
# INIT { eval q/$x = 43/ }
INIT { $x = 43 }
$x = bless {}, "X";
say "x REFCNT​: ", B​::svref_2object( \$x )->REFCNT;
undef($x) if grep { /force/ } @​ARGV;
}

say "x REFCNT​: ", eval { B​::svref_2object( \$x )->REFCNT };

Note that this is a differerent (and non-lexical) $x.

say $ok ? "ok" : "not ok";

The (non-eval) INIT block is a closure on $x, so holds a reference to it.

Note that perlmod specifically says with respect to BEGIN blocks​:
"Once a BEGIN has run, it is immediately undefined and any code it
used is returned to Perl's memory pool." but no such thing happens
with INIT blocks.

So I'd have to say this is a limitation, not a bug. But that doesn't
mean it is a desired one :)

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 5, 2015

From @toddr

On Tue Nov 03 09​:49​:28 2015, sthoenna@​gmail.com wrote​:

The (non-eval) INIT block is a closure on $x, so holds a reference to
it.

Note that perlmod specifically says with respect to BEGIN blocks​:
"Once a BEGIN has run, it is immediately undefined and any code it
used is returned to Perl's memory pool." but no such thing happens
with INIT blocks.

So I'd have to say this is a limitation, not a bug. But that doesn't
mean it is a desired one :)

So to rephrase what we're saying here​: We're saying that any variable used in an INIT block like this either needs to be weakened or will never be destroyed until global destruction? I'm inclined to say that's a problem. At the least it should be documented right?

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

No branches or pull requests

2 participants