|
I looked into this a bit more. The reason your HashBlessed class seems to work is that it does not have a DESTROY subroutine. If you add:
sub DESTROY {
delete(shift->{data});
}
you'll find that HashBlessed fails, too. This shows that shared objects stored inside shared structures get destroyed inappropriately. As I stated before, this is a threads::shared bug. The following illustrates this (without using OIO):
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
package Jar; {
my @jar :shared;
sub new
{
bless(&threads::shared::share({}), shift);
}
sub store
{
my ($self, $cookie) = @_;
push(@jar, $cookie);
print("JAR : Cookie stored\n");
return $jar[-1]; # BUG: The cookie is destroyed here
}
}
package Cookie; {
my $destruction_count = 0;
sub new
{
bless(&threads::shared::share({}), shift);
}
sub DESTROY
{
$destruction_count++;
print("COOKIE: destruction count = $destruction_count\n");
}
}
package main;
MAIN:
{
my $jar = Jar->new();
my $cookie = Cookie->new();
print("MAIN : Storing cookie\n");
$jar->store($cookie);
print("\nMAIN : Cookie should not have been destroyed yet\n");
print("\nMAIN : Exiting scope\n")
}
print("\nDONE\n");
which outputs:
MAIN : Storing cookie
JAR : Cookie stored
COOKIE: destruction count = 1
MAIN : Cookie should not have been destroyed yet
MAIN : Exiting scope
COOKIE: destruction count = 2
DONE
|