我正在尝试删除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;发布于 2015-06-09 09:41:47
最常犯的错误是在仍然存在引用父GlFreeForm的GLProxyObject的情况下尝试删除GlFreeForm。因此,清除场景的最佳解决方案是首先将所有GLProxyObject的MasterObject参数设置为空。为了避免阻塞对象,建议使用单个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年的大量使用中,我对该代码从未遇到过任何问题,所以请随意使用它。
https://stackoverflow.com/questions/22857196
复制相似问题