# Unexpected - Reference to deleted Group

#1

Hi,

I am getting the error ‘Reference to deleted group’ when I run the following code (this is just a simplified extract). In a nutshell: I need to calculate the area of several polygons; to do this I create a face within a group, use the Face.area method and then delete the group. As you can see the group never gets deleted, nevertheless after a few successful iterations I receive the error.

``````nusselt = mod.entities.add_group
(1..nTilt).each do |i|
(1..nAzimuth).each do |j|
theta = Math::PI / 2.0 - (tiltMin + (i * 1.0 - 1.0) * dTilt)
phi = azimuthMin + (j * 1.0 - 1.0) * dAzimuth
pt1 = pt1(theta, phi)
pt2 = pt2(theta, phi)
pt3 = pt3(theta, phi)
pt4 = pt4(theta, phi)
if (projection != nil)
view_factor[i-1][j-1] = projection.area / Math::PI
else
view_factor[i-1][j-1] = 0.0
end
end
end
``````

I have tried several cases and i can’t find a pattern: sometimes it happens at the first iteration, sometimes at an apparently random point in the loop.

Any help or suggestion?

Thanks

Reference to deleted Group
#2

If you’re group is empty, Sketchup cleans it up by deleting the empty group.
Alway check if the group is still valid and if it’s not create a new one.

``````nusselt = mod.entities.add_group
(1..nTilt).each do |i|
(1..nAzimuth).each do |j|
theta = Math::PI / 2.0 - (tiltMin + (i * 1.0 - 1.0) * dTilt)
phi = azimuthMin + (j * 1.0 - 1.0) * dAzimuth
pt1 = pt1(theta, phi)
pt2 = pt2(theta, phi)
pt3 = pt3(theta, phi)
pt4 = pt4(theta, phi)
nusselt = mod.entities.add_group if nusselt.deleted?
if (projection != nil)
view_factor[i-1][j-1] = projection.area / Math::PI
else
view_factor[i-1][j-1] = 0.0
end
end
end``````

#3

Hi Thanks. Will check now. I just wonder how can a group ‘become’ empty after a few iterations. In my example some of the points will not generate a face and that is why I have the control.

#4

It does work. Thanks

#5

In my opinion this is a bug, most likely due to over-aggressive clean up inside Entities#add_face. nusselt holds an active reference to the Group, so this can’t be the Ruby garbage collector in action, it has to be the SketchUp API, and that method is the only API call you make. The fact that your code has not yet successfully added anything to the nusselt Group’s Entities collection should not make it a candidate for deletion. How does the API know that a later iteration of your loop will not succeed in adding a valid Face?

#6

Do you have a sample snippet that is complete and ready to be run in the console?

Another thing: are you wrapping your code in model.start/commit_operation? If you are not then that could be the issue as you might be triggering SU’s cleanup functions by not marking the start and end of your operation.

#7

Thanks for the replies. I have changed my code so that I don’t need to create a group. All the polygons I generate have 4 vertices. To calculate the area - my original goal - I simply use the classical formula from computational geometry. A bit slower maybe, but less headaches

That part of the code is not between model.start/commit_operation.

#8

Could you quickly try that to see if it makes a difference?

#9

Actually, I have just realized that I had a mod.start_operation at the beginning

``Sketchup.active_model.start_operation("mesh",true``

#10

Hmm… then this is odd. Can you make a complete standalone example that reproduce this?

#11

This piece of code works by itself. The fact is that now it is giving me a different error. I must have made some intermediate change and forgot to commit!

Thanks
Ruggiero

#12

I get a duplicate points error when I paste that snippet into the console:

``````get_shadow_mask(30.0, 45.0)
Error: #<ArgumentError: Duplicate points in array>
<main>:32:in `each'