Code crashes at iteration 86

蓝咒 提交于 2019-12-20 03:12:08

问题


static void Job47(Args _args)
 {
 str                                  path,stx;
 TreeNodeIterator                     iter;
 TreeNode                             treeNode, treeNodeToRelease;
 Map                                  dictMenuDisplay;
 FormName                             formName;
 MenuItemName                         menuItemName;
 container                            conMenu;
 int                                  i,n;
   ;

 for (n=1;n<=100;n++)
       {
        info(strfmt("iter:%1",n));
        path            ="Menu Items\\Display";
        dictMenuDisplay = new Map(Types::String,Types::Container);
        treenode        = Treenode::findNode(path);
        iter            = treenode.AOTiterator();
        treenode        = iter.next();

        while (treenode)
          {
          formName     = treenode.AOTgetProperty("Object");
          menuItemName = treenode.AOTname();

           if (dictMenuDisplay.exists(formName))
            {
             conMenu = dictMenuDisplay.lookup(formName);
             conMenu = conIns(conMenu,conlen(conMenu)+1,menuItemName);
             dictMenuDisplay.insert(formName,conMenu);
             }

           else
             dictMenuDisplay.insert(formName,[menuItemName]);
                      //  treenode = iter.next();

           if(treeNodeToRelease && SysTreeNode::isApplObject(treeNode))
            {
             treeNodeToRelease.treeNodeRelease();
             treeNodeToRelease=null;
            }

           if(SysTreeNode::isApplObject(treeNode))
            {
              treeNodeToRelease=treeNode;
            }

            treeNode=iter.next();
        }
    }
  }

I get the error "overflow in internal run stack",the code runs till 86th iteration correctly, help...


回答1:


The kernel doesn't immediately garbage collect TreeNode objects. Once you are done with a treenode for an application object and all it's children, you need to call TreeNode.treeNodeRelease(), followed by treeNode=null; to let the garbage collector clean up.

...
TreeNode treeNodeToRelease;
...

while(treenode)
{

    ... /* do stuff with treenode */

    if(treeNodeToRelease && SysTreeNode::isApplObject(treeNode))
    {
        treeNodeToRelease.treeNodeRelease();
        treeNodeToRelease=null;
    }
    if(SysTreeNode::isApplObject(treeNode))
    {
        treeNodeToRelease=treeNode;
    }
    treeNode=iter.next();
}



回答2:


The code:

 if(treeNodeToRelease && SysTreeNode::isApplObject(treeNode))

should be replaced by:

 if (treeNodeToRelease)

The release of the object should not depend on the next object.

or maybe this is enough (supplanting Jay's solution):

while (treenode)
{
    ... /* do stuff with treenode */
    treeNode.treeNodeRelease();
    treeNode = iter.next();
}



回答3:


Quote from the manual:

If you run this method (treeNodeRelease()) on many tree nodes in the same execution, it can be demanding on resources. You should unload the tree nodes as you go along to give the garbage collector a chance to remove them.

Make sure to remove all references to the tree node and its subnodes before you call this method.

Also you have at least 85 references of the treeNodeToRelease (and treeNode!) floating around -> call treeNodeToRelease.treeNodeRelease() at the end of your outer loop.



来源:https://stackoverflow.com/questions/5103429/code-crashes-at-iteration-86

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!