draco icon indicating copy to clipboard operation
draco copied to clipboard

Bug?: Different Component instances sharing same attribut instance

Open Dahie opened this issue 4 months ago • 0 comments

Hi,

I just took 2 hours debugging a weird behavior, I could nail down to different Components instances sharing attribute models.

You can reproduce it like this:

Open an simple Ruby console, require "draco.rb" and run this code:

class A < Draco::Component
  attribute :list, default: []
end

class B < Draco::Entity
  component A
end

class C < Draco::Entity
	component A
end

b1 = B.new
b2 = B.new
c = C.new
puts b1.a.list.object_id
puts b2.a.list.object_id
puts c.a.list.object_id
puts b1.a.list == b2.a.list
puts b1.a.list == c.a.list

the list object in instances b1, b2 and c is the same. Adding something to the list in b1.a will have it added in c.a

b.a.list << "Example"
#=> ["Example"]
c.a.list
#=> ["Example"]

The same happens on other types, like a Hash.

I'm working around this now by using Entity#after_initialize like this:

class B < Draco::Entity
  component :A

  def after_initialize
    a.list = []
  end
end

Is this expected behavior or a bug? I got caught by surprise.

Regards, Dahie

Dahie avatar Jan 01 '26 15:01 Dahie