Problem with Marshaling WeakRefs?

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

On Thu, Dec 07, 2006 at 08:55:10AM +0900, [email protected] 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.

Hi,

In message “Re: Problem with Marshaling WeakRefs?”
on Thu, 7 Dec 2006 08:55:10 +0900, [email protected] writes:

|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?

No, it’s a bug. Thank you for finding it. Note that restored weakref
object will weakly refer to a new (copied) object. It’s only way to
make weakref valid between processes.

						matz.

Logan C. wrote:

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…

Yukihiro M. wrote:

|
|Am I missing something?

No, it’s a bug. Thank you for finding it. Note that restored weakref
object will weakly refer to a new (copied) object. It’s only way to
make weakref valid between processes.

  					matz.

By “will” do you mean “should”? Because it looks to me at the moment as
if there’s no ref at all there. Or have I mis-understood?

And does “newly copied object” mean that instead if I had two weak refs
to an object in my original program, then after loading they’ll refer
to separate ones???

Thanks for the fast response - and btw: great language design!