Problem with Marshaling WeakRefs?

U

umptious

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, :eek:wner)
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
 
L

Logan Capaldo

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.
 
U

umptious

Logan said:
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..
 
U

umptious

Yukihiro said:
Hi,

In message "Re: Problem with Marshaling WeakRefs?"
on Thu, 7 Dec 2006 08:55:10 +0900, (e-mail address removed) 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.

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top