api-issue-tracker
api-issue-tracker copied to clipboard
Crash cause by remove_observer
- SketchUp/LayOut Version: SketchUp 2024
- OS Platform: Windows
When you save a sketchup component, observers are incorrectly notified that new components have been added. Adding new observers to this definition and later removing them seems to cause crashes.
The below snippet expects to be used with the 2024 default template with the "Teddy" face me component
# class definitions
class DefinitionsObserver
def initialize(model)
@definition_observers = []
model.definitions.add_observer(self)
model.definitions.each { |definition|
observe_definition(definition)
}
end
def onComponentAdded(definitions, definition)
observe_definition(definition)
end
def observe_definition(definition)
@definition_observers.push(DefinitionObserver.new(definition))
end
def disable
@definition_observers.each(&:disable)
end
end
class DefinitionObserver
def initialize(definition)
@definition = definition
@definition.add_observer(self)
end
def disable
@definition.remove_observer(self)
end
end
# setup observers
active_model = Sketchup.active_model
definitions_observer = DefinitionsObserver.new(active_model)
# Setup a dummy definition
foo = active_model.definitions.add("foo")
foo.entities.add_edges([0,0,0],[10,10,10])
# Add instance to "Teddy"
active_model.definitions["Teddy"].entities.add_instance(foo, Geom::Transformation.new)
# At this point right click on teddy, select "Save as" and save the component somewhere
# This triggers onComponentAdded for the "foo" component definition again, attaching a second observer
definitions_observer.disable # this will cause a crash when attempting to remove "foo"'s second observer
Logged as: SKEXT-4155