Title: RE: PerlHack - Adding new flag into a SV

> From: "Orton, Yves" <[EMAIL PROTECTED]>
> > > I thought you are just automagicaly weaken()ing the referenced
> > > stored in the tied hash??? Something like package
> Hash::NoRef; use
> > > Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash);
> > > sub STORE {       $_[0]{$_[1]} = $_[2];  
> weaken($_[0]{$_[1]}) if (ref
> > > $_[2]);
> > > }
> > > 'And that\'s it!';
> > > Am I missing something?
> >
> > Itll segfault under many circumstances.
> >
> > yves
>
> Then you should report an error in Scalar::Util.
>
> Do you have an example script that segfaults?

No but I can describe one that would. Imagine a binary tree. We store each node using weakrefs in the structure you describe. Now I search and find an internal node of the tree. The root of the tree falls out of scope, essentially "stripping" the tree of all of its relatives. Now the hash contains a zillion refs to non existant objects. Access one of those and POOF!

Maybe you were thinking I meant a problem with your code. I meant a problem with the entire approach. IMO weak refs are not the greatest solution to the problems that they are usually used for. Often there are much better approaches that don't involve risking segfaults.

Anyway, :-)
Yves


Reply via email to