[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem with Marshaling WeakRefs?

umptious

12/6/2006 11:55:00 PM

I'm experimenting with Marshal, but I'm having problems making it work
with data structures that use WeakRefs. Marshal saves without
complaining, but the WeakRefs seem to be junk when I load. Hopefully
the program below explains everything. I'm using Ruby 184-15 on Windows
XP Pro.

Am I missing something?

----------------------------------------------------------------------------------------------------------------------------

#!/usr/bin/env ruby

require "WeakRef"

save = false

class Box
def initialize()
@things = []
end

def add(q)
@things.push q
q.set_owner(self)
end

attr_reader :things
end

class Pen
def initialize(name)
@name = name
@owner = nil
end
def set_owner(owner)
@owner = WeakRef.new(owner)
end
attr_reader(:name, :owner)
end

if save
box = Box.new()
[Pen.new("Parker 51"), Pen.new("Densho"), Pen.new("Snorkel")].each{
|q| box.add(q) }
File.open('marsh.txt', "w") do |f|
Marshal.dump(box, f)
end
else
File.open("marsh.txt") do |f|
box = Marshal.load(f)
end
end

p box.things[0].owner # Error occurs here...

#c:/ruby/lib/ruby/1.8/WeakRef.rb:64:in `_id2ref': no implicit
conversion
# from nil to integer (TypeError)
# from C:/RubyCode/Adventure.04/scratch.rb:47:in `p'
# from C:/RubyCode/Adventure.04/scratch.rb:47

2 Answers

Logan Capaldo

12/7/2006 12:19:00 AM

0

On Thu, Dec 07, 2006 at 08:55:10AM +0900, umptious@gmail.com wrote:
> I'm experimenting with Marshal, but I'm having problems making it work
> with data structures that use WeakRefs. Marshal saves without
> complaining, but the WeakRefs seem to be junk when I load. Hopefully
> the program below explains everything. I'm using Ruby 184-15 on Windows
> XP Pro.
>
> Am I missing something?
>
WeakRefs should be junk when you load, if they're allowed to be
Marshaled at all. A WeakRef is a reference that's not tracked by GC,
unless by a very strange odd coincidence no weakref from one invocation
of a program should be valid in another.

umptious

12/7/2006 1:40:00 AM

0


Logan Capaldo wrote:
> On Thu, Dec 07, 2006 at 08:55:10AM +0900, umptious@gmail.com wrote:
> > I'm experimenting with Marshal, but I'm having problems making it work
> > with data structures that use WeakRefs. Marshal saves without
> > complaining, but the WeakRefs seem to be junk when I load. Hopefully
> > the program below explains everything. I'm using Ruby 184-15 on Windows
> > XP Pro.
> >
> > Am I missing something?
> >
> WeakRefs should be junk when you load, if they're allowed to be
> Marshaled at all. A WeakRef is a reference that's not tracked by GC,
> unless by a very strange odd coincidence no weakref from one invocation
> of a program should be valid in another.

If this is correct, it's still a bug until it is VERY clearly
documented..