RE: PerlHack - Adding new flag into a SV

2004-04-27 Thread Orton, Yves
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






Re: PerlHack - Adding new flag into a SV

2004-04-27 Thread Adrian Howard
On 27 Apr 2004, at 13:01, Orton, Yves wrote:
[snip]
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!
[snip]

Erm. I don't think so. If the thing a weakref points to is collected 
you get a pointer to undef. To quote from the docs:

	REF will be turned into a weak reference. This means that it 	not hold 
a reference count on the object it references. Also 	the reference 
count on that object reaches zero, REF will be set 	undef.

If you're getting something that can cause a segfault then something's 
wrong somewhere surely.

Adrian



Re: PerlHack - Adding new flag into a SV

2004-04-27 Thread Sam Vilain
No, weakrefs cast backref magic onto their target to avoid this 
problem.  When the reference count drops to zero the magic ensures that 
the weakref is set to undef.

See Perl_sv_rvweaken() in sv.c for more.

Sam.

Orton, Yves wrote:

 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?

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



--
Sam Vilain, sam /\T vilain |T net, PGP key ID: 0x05B52F13
(include my PGP key ID in personal replies to avoid spam filtering)