ObjectSpace::_id2ref and GC

A

Ara.T.Howard

say i'm doing something like this


class << self

def attribute(*list)

list.flatten!

list.each do |arg|
@attrbiutes ||= []
@do_not_gc ||= []

list.flatten.each do |arg|
arg, default =
case arg
when Hash
arg.to_a.first
else
[arg, nil]
end

@do_not_gc << default unless @do_not_gc.include? default

unless instance_methods.include? "#{ arg }"
module_eval <<-code
def #{ arg }(*a)
unless defined? @#{ arg }
@#{ arg } = ObjectSpace::_id2ref #{ default.object_id }
end
if a.empty?
@#{ arg }
else
send('#{ arg }=', *a)
end
end
def #{ arg }=(val, *a)
if a.empty?
@#{ arg } = val
else
@#{ arg } = ([val] + a)
end
end
def #{ arg }?
defined? @#{ arg } and @#{ arg }
end
code
end

@attributes << "#{ arg }"
end

@attributes
end

end


which allows niceness such as

class C
attribute 'foo' => 42
end

is the @do_not_gc required? it seems like doing something like

id = object.object_id

and then later

value = ObjectSpace::_id2ref id

one is gambling that the original object has not been gc'd and that the id
still points to the same object. is this accurate?

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: ObjectSpace::_id2ref and GC"

|it seems like doing something like
|
| id = object.object_id
|
|and then later
|
| value = ObjectSpace::_id2ref id
|
|one is gambling that the original object has not been gc'd and that the id
|still points to the same object. is this accurate?

I'm sorry I'm not sure I get what you meant. You call

id = object.object_id

and then later

value = ObjectSpace::_id2ref id

the original object may or may not be reclaimed. There's no
guarantee. Unless you check reclamation by finalizer (as in
weakref.rb), you can't tell whether value is the original object or
a recycled object.

matz.
 
A

Ara.T.Howard

Hi,

In message "Re: ObjectSpace::_id2ref and GC"

|it seems like doing something like
|
| id = object.object_id
|
|and then later
|
| value = ObjectSpace::_id2ref id
|
|one is gambling that the original object has not been gc'd and that the id
|still points to the same object. is this accurate?

I'm sorry I'm not sure I get what you meant. You call

id = object.object_id

and then later

value = ObjectSpace::_id2ref id

the original object may or may not be reclaimed. There's no
guarantee. Unless you check reclamation by finalizer (as in
weakref.rb), you can't tell whether value is the original object or
a recycled object.

exactly what i wanted to know!

thanks.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top