Bug #9938
closedObjectSpace::allocation_class_path does not object's class
Description
Possible bug in ObjectSpace::allocation_class_path
returning nil
instead of expected class name:
require 'objspace'
class A
def foo
ObjectSpace::trace_object_allocations do
obj = Object.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = Array.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = []
p ObjectSpace::allocation_class_path(obj) # prints "A" as expected !!
obj = obj.dup
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead !!
obj = Hash.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = {}
p ObjectSpace::allocation_class_path(obj) # prints "A" as expected !!
end
end
end
A.new.foo
Very inconsistent behavior, allocation_class_path
seems to works only with objects defined with literals not instantiated with class.new
method.
Also discussed at StackOverflow:
http://stackoverflow.com/questions/24181789/objectspaceallocation-class-path-not-working-as-expected
Updated by dunric (David Unric) over 10 years ago
I'm sorry about code formatting. Bellow is a fixed version:
require 'objspace'
class A
def foo
ObjectSpace::trace_object_allocations do
obj = Object.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = Array.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = []
p ObjectSpace::allocation_class_path(obj) # prints "A" as expected !!
obj = obj.dup
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead !!
obj = Hash.new
p ObjectSpace::allocation_class_path(obj) # "A" expected, prints nil instead
obj = {}
p ObjectSpace::allocation_class_path(obj) # prints "A" as expected !!
end
end
end
A.new.foo
Updated by ko1 (Koichi Sasada) over 10 years ago
Hi,
Thank you for your report.
Please add this magic line:
ObjectSpace.each_object(Module){|o| o.name}
at the beggining of script. It will show the correct output.
This is because of Module naming cache issue.
I want to fix this issue for 2.2.
But 2.1, please left it as a spec.
(we can acquire a class name if we had a luck)
Updated by dunric (David Unric) over 10 years ago
Wow, that was fast !
I've been stuck at rb_class_path_cached
from variable.c line #281
failing to look up a class_path. Glad it would be fixed in 2.2 .
Good job.
Take care.
Updated by nobu (Nobuyoshi Nakada) over 10 years ago
- Description updated (diff)
Updated by ko1 (Koichi Sasada) over 10 years ago
- Status changed from Open to Rejected