0

我正在尝试删除 GLScene 容器中的所有场景对象,并使用以下代码片段来执行此操作,但由于某些未知原因,当我尝试释放对象时,它崩溃会引发分段错误。

我尝试逐行调试,它发现不知何故容器类Glscene1.Objects[i]包含一些具有“#2”类名的莫名其妙的类。我尝试在不调用 Free 方法的情况下运行相同的代码片段,然后没有发生任何异常,对象没有被删除,但对象类名称保持一致。

for i := 0 to GLScene1.Objects.Count - 1 do
  begin
     if (not GLScene1.Objects[i].ClassNameIs('TGLCamera')) and
        (not GLScene1.Objects[i].ClassNameIs('TGLLightSource')) and
        (not GLScene1.Objects[i].ClassNameIs('TGLDummyCube')) and
        (not GLScene1.Objects[i].ClassNameIs('TGLXYZGrid')) and
        (not GLScene1.Objects[i].ClassNameIs('TGLSceneRootObject')) then
     begin
//      if GLScene1.Objects[i].Count > 0 then
//      GLScene1.Objects[i].DeleteChildren;
      GLScene1.Objects.Remove(GLScene1.Objects[i],false);

      if GLScene1.Objects[i] <> nil then    // I comment out these lines 
        GLScene1.Objects[i].free;           // I comment out these lines 
     end;

  end;
4

1 回答 1

0

最常犯的错误是当仍然存在引用父 GlFreeForm 的 GLProxyObject 时尝试删除 GlFreeForm。所以清除场景的最佳解决方案是先将所有 GLProxyObject 的 MasterObject 参数设置为 nil。为避免阻塞对象,建议使用单个 GLDummyCube(本例中为 GLDummyCube1)作为所有其他场景对象的根对象:

if Form1.GLDummyCube1.Count>0 then
begin
  for I := (Form1.GLDummyCube1.Count-1) downto 0 do
  begin
    if (Form1.GLDummyCube1.Children[I].ClassNameIs('TGLProxyObject')) then
    begin
      TGLProxyObject(Form1.GLDummyCube1.Children[I]).MasterObject := nil;
    end;
  end;

  while (Form1.GLDummyCube1.Count>0) do
  begin
    try
      Form1.GLScene1.FindSceneObject(Form1.GLDummyCube1.Children[0].Name).Destroy;
    except
      //inform error, but I never had one
    end;
  end;
end;

在长达 4 年的大量使用中,我从未遇到过该代码的任何问题,所以请随意使用它。

于 2015-06-09T01:41:47.007 回答