TypeError when subtract is used

An extension I’m developing uses subtract to edit two component instances.
This method is described in the Ruby API.

But when I apply the method in the code, an error is shown 2 times:
Error: #<TypeError: reference to deleted Entity>

Also, when I apply the below code snippit from the API, I get the following error:

# Code from Ruby API

entities = Sketchup.active_model.entities
instance1 = entities[0]
instance2 = entities[1]
result = instance1.subtract(instance2)
# Result Ruby Console

Error: #<TypeError: reference to deleted Entity>
<main>:3:in `attribute_dictionaries'
<main>:3:in `onComponentAdded'
<main>:3:in `subtract'
<main>:3:in `<main>'
SketchUp:in `eval'
Error: #<TypeError: reference to deleted Entity>
<main>:3:in `attribute_dictionaries'
<main>:3:in `onComponentAdded'
<main>:3:in `subtract'
<main>:3:in `<main>'
SketchUp:in `eval'
=> #<Sketchup::Group:0x0000015e87548af8>

Am I overlooking something that is causing this error message?

Most likely it is caused by your specific modell (share it, and someone can check).

For me it works (Windows 10, SU2021)


Here is a demo model.
I have it with all subtract operations in different models.

SU22_subtract demo.skp (105.0 KB)

In any case, good to see that it works for you in SketchUp 2021.
Now I still have to find out why I do get the error message in SketchUp Pro 2022.

I just checked your model and got a similar result as my animation above. (Still in SU2021, I can test later today with 2022 at home)

Reading your error messages, you may have an Extension, running an observer in a background…?

Thanks for the quick responses @dezmo.

I just turned off all extensions.
Then the error message was not displayed.

Turned on all extensions one by one and it appears that Enscape, at this point, is triggering the error message.

(Nevermind ... I see you've traced it to Enscape.)

The message indicates that an error is occurring within the onComponentAdded callback observer of a DefinitionsObserver object.

Do you have an observer attached to any of the definitions involved in the boolean subtract operation ?

And if so, does the callback try to call #attribute_dictionaries upon one of the arguments or some other reference your extension is keeping that might have “gone out of scope” (ie, become invalid) ?

There is a recently reported issue with some observers regarding attribute dictionaries that shouldn’t happen. I’m trying to find the issue report.

I’m guessing that if you wrapped your code in model.start_operation and model.commit_operation you’d not see this issue, as observers are suspended and queued while an operation is active.