-->

Code crashes at iteration 86

2019-02-25 15:35发布

问题:

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.



标签: axapta x++