develooper Front page | perl.perl5.porters | Postings from October 2000

Reference counts on self-tied objects

Thread Previous | Thread Next
Randy J. Ray
October 31, 2000 16:12
Reference counts on self-tied objects
Message ID:
In my Perl-RPM package, it's been recently pointed out to me that the
object destructors aren't getting called when the object undef'd, but
rather at the end of the script (well, the bug report focused on a
different problem, but this is the source of it).

I've done testing and confirmed this. My guess is that something in the
nature of the self-tying object is causing the ref-count to be higher, and
I'm not successful in getting it sufficiently down. My TIEHASH method is
pure XS, and the new method looks like this:

sub new
    my $class = shift;
    my %hash = ();

    tie %hash, $class, @_;
    return (tied %hash);

Even though I thought this would work (with %hash going out of scope), I
also tried explicitly untie'ing the hash after assigning the value:

sub new
    my $class = shift;
    my %hash = ();

    tie %hash, $class, @_;
    my $self = tied %hash;
    untie %hash;
    return $self;

This didn't do it, either. Plus, if in my script I forego the object ref
entirely, the destructor isn't called when "untie" is invoked. The relevant
typemap OUTPUT entry for these are:

        if ($var)
            $arg = sv_bless(newRV_noinc((SV*)$var),
                            gv_stashpv(\"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
            hv_magic($var, (GV *)Nullhv, 'P');
            $arg = newSVsv(&PL_sv_undef);

(This handles both RPM::Database and RPM::Header)

Any help is appreciated. When someone untie's a tied hash in my extension,
I'd really like it if it happened *then*, rather than at script-end.

Randy J. Ray      | Programming is a Dark Art [...] The programmer is fighting  | against the two most destructive forces in the universe:
415-777-9810 x246 | entropy and human stupidity. --Dr. Damian Conway

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About