Project

General

Profile

Actions

Bug #11360

closed

Singleton class doesn't appear by ObjectSpace.each_object

Added by ko1 (Koichi Sasada) almost 9 years ago. Updated almost 9 years ago.

Status:
Closed
Target version:
-
[ruby-core:70006]

Description

Singleton class doesn't appear by ObjectSpace.each_object.

The following is reproducible code.

class C
  class << self
    p [self, self.class]
    $c = self
  end
end

ObjectSpace.each_object(Class){|o|
  exit if $c == o
}
raise "#{$c} is not found!"

This is because internal_object_p in gc.c skips singleton classes.

static int
internal_object_p(VALUE obj)
{
    RVALUE *p = (RVALUE *)obj;

    if (p->as.basic.flags) {
	switch (BUILTIN_TYPE(p)) {
	  case T_NONE:
	  case T_IMEMO:
	  case T_ICLASS:
	  case T_NODE:
	  case T_ZOMBIE:
	    break;
	  case T_CLASS:
	    if (FL_TEST(p, FL_SINGLETON))
	      break;
	  default:
	    if (!p->as.basic.klass) break;
	    return 0;
	}
    }
    return 1;
}
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0