# Problem with Marshaling WeakRefs?

**URL:** <https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577>\
**Category:** Ruby\
**Created:** [December 7, 2006, 12:56am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577 "2006-12-07T00:56:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 12:56am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577/1 "2006-12-07T00:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 1:19am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577/2 "2006-12-07T01:19:01Z")

</div>

On Thu, Dec 07, 2006 at 08:55:10AM +0900, removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 1:45am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577/3 "2006-12-07T01:45:03Z")

</div>

Hi,

In message “Re: Problem with Marshaling WeakRefs?”  
on Thu, 7 Dec 2006 08:55:10 +0900, removed\_email\_address@domain.invalid 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.
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 2:41am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577/4 "2006-12-07T02:41:08Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 2:47am UTC](https://www.ruby-forum.com/t/problem-with-marshaling-weakrefs/78577/5 "2006-12-07T02:47:08Z")

</div>

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!
