Posted on 2007-10-26 14:21:59-07 by jdhedden in response to 6334
Re: Storing objects inside of shared objects
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
Direct Responses: 6340 | Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.