Error: Reference to deleted entities
-
Hello, I've recently picked up scripting for SketchUp. I'd refer to myself as an intermediate ruby programmer.
I'm working on a tool that draws standardized screws. Today I've come across a very strange error, namelyError: #<TypeError: reference to deleted Entities>
C:/Users/Christopher/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/DS_SteelProfiler/screwset.rb:1218:in `add_group'This is very odd and I can't explan it. In said line is written:
group = ent_screw_thread.add_group
The error suggests the entities object that I want the group to be in was deleted. But this is impossible because
ent_screw_thread
has been initialized and was not touched once since then. The initialization in line 930 + 931 looks like this:def_screw_thread = def_list.add ("Screw thread") ent_screw_thread = def_screw_thread.entities
However, I had it working before! This was before I made changes to the code in line 1173 to 1204. Again, the entitiy object is not touched in any line until 1218 where I immediately get the error when trying to access it, so no matter what's written in that part it should have no influence on the entities object. But when I remove said lines, the program magically works again. Here is the code of these lines:
` circle = ent_screw.add_circle [@l_head, 0, 0], vec0, (@d_shaft/2 + @r), 24
p1 = [(@l + @l_head), 0, 0]
p2 = [(@l + @l_head), 0, (@m/2 - thread_depth)]
p3 = [(@l_to_thread + @l_head), 0, (@m/2 - thread_depth)]
p4 = [(@l_shaft + @l_head), 0, (@m/2)]
p5 = [(@r + @l_head), 0, (@m/2)]
p6 = [@l_head, 0, (@m/2 + @r)]
p7 = [@l_head, 0, 0]arr_l = Array.new arr_l = ent_screw.add_edges p1, p2, p3, p4, p5 arc = ent_screw.add_arc [(@l_head + @r), 0, (@d_shaft/2 + @r)], Geom::Vector3d.new(0, 0, -1), Geom::Vector3d.new(0, 1, 0), @r, 0.degrees, 90.degrees, 6 arc.each do |e| arr_l << e end l1 = ent_screw.add_line p6, p7 l2 = ent_screw.add_line p7, p1 arr_l << l1 arr_l << l2 face = ent_screw.add_face arr_l #face.followme circle`
Notice how these lines are working with
ent_screw
, notent_screw_thread
. These are two different entities objects from different components.screw_thread
is supposed to become a subcomponent ofscrew
, but this is done at the end of the program and works flawless. So my question is, why isent_screw_thread
getting deleted when I put these lines back into the code? Any help or clues would be greatly appreciated, I'm slowly getting insane with this errorThanks.
-
You haven't [thankfully] given all of your code...
BUT, if you add a new group into an entities-context, but you leave its own entities empty, then SketchUp's 'garbage-collection' can sometimes delete that empty group [any empty groups/component-definitions are auto-purged by SketchUp's GC], so then later on when you try to add content to its entities you get an error message saying it's no longer valid [deleted]...
So, either use add_cpoint into the new group.entities immediately you create that otherwise empty group to stop the GC [you can erase the cpoint later when there is geometry added], OR more logically, only add that new group immediately before you want to add content into group.entities - thus giving no time for SketchUp's GC to kick in and spot the momentarily empty group... -
Also do not put a space between the method name and the parenthesis surrounding it's arguments. It is no longer allowed in Ruby 2.x.
Also it is now convention to always use parenthesis around method call argument lists, except for global methods (those defined in module
Kernel
or classObject
, and any added "legally" to them such as those in "sketchp.rb", RubyGems or standard library gems.)
They are said to have "keyword status". -
Thank you for the replies! I managed to fix it on my own, the problem was that a small mistake in the lines 1173 to 1204 caused the creation of the face to fail. I still can't explain why this would affect the other entities object, though. Oh well, now it's all working fine so I better don't bother
@tig said:
You haven't [thankfully] given all of your code...
BUT, if you add a new group into an entities-context, but you leave its own entities empty, then SketchUp's 'garbage-collection' can sometimes delete that empty group [any empty groups/component-definitions are auto-purged by SketchUp's GC], so then later on when you try to add content to its entities you get an error message saying it's no longer valid [deleted]...
So, either use add_cpoint into the new group.entities immediately you create that otherwise empty group to stop the GC [you can erase the cpoint later when there is geometry added], OR more logically, only add that new group immediately before you want to add content into group.entities - thus giving no time for SketchUp's GC to kick in and spot the momentarily empty group...But I'm not using groups unless I want its content not to touch the existing geometry to avoid automatic "glueing to each other" or however it's called when edges share a vertex and stick to other edges. So, I'm not using groups in empty entities objects, nor am I leaving empty groups anywhere. Thanks anyway for the hint!
@dan rathbun said:
Also do not put a space between the method name and the parenthesis surrounding it's arguments. It is no longer allowed in Ruby 2.x.
Also it is now convention to always use parenthesis around method call argument lists, except for global methods (those defined in module
Kernel
or classObject
, and any added "legally" to them such as those in "sketchp.rb", RubyGems or standard library gems.)
They are said to have "keyword status".But I'm not leaving spaces when I call method(arg0, arg1, ...), the only times I have spaces is when I initialize a new point or vector on the spot, like method [0, 1, 0], arg1, ... ; but in this case the parenthesis "[]" are used to make an array of values that will be auto-converted to point3d or vector3d by SketchUp, or am I wrong?
I didn't know parenthesis are now required by convention, though. Good to know.Here is the finished screw, just need to fix one or two small bugs...
-
@dubai_skyscraper said:
The initialization in line 930 + 931 looks like this:
def_screw_thread = def_list.add ("Screw thread") ent_screw_thread = def_screw_thread.entities
I really thought that looked like a space, between the
.add
and the("Screw thread")
,...It could be a "vacuum" character though.
P.S.
are brackets, { } are braces, ( ) are parenthesis.
Advertisement