Book a Demo

Author Topic: Recursive method inside foreach failed  (Read 3807 times)

Kezia

  • EA User
  • **
  • Posts: 33
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Recursive method inside foreach failed
« on: October 31, 2008, 05:14:59 pm »
Hi,

I'm creating an add-in for EA and I found a weird symptom.
I have foreach loop with recursive method inside. This code made the EA crash, each time it is executed (Error Report from Microsoft will pop up, letting me choose whether  want to send the error). This case only happen if  I make a build in Release mode and install it to my computer (and other computer too).

BUT, if I create the build with Debug mode. The code runs successfully.

After I change "foreach" loop into "for" loop, the release version build works fine.

I was thinking that it might be an .Net's bug on looping through collection. But I have tested recursive method inside "foreach" with another collection other than EA collection (i.e. Directory.GetDirectories()), it runs successfully.

So I begin to think that it might be an EA collection's bug. I'm not sure about this, but I think it's good to let the other know to avoid the same problem, coz it is really hard to find where the error is and tooks me 2 weeks to solve the problem.
Here is the link who has the same problem as me.

http://social.msdn.microsoft.com/Forums/en-US/clr/thread/9a4e8bbb-efbd-4a90-9549-4e1795198800/

Here is my code before :

Code: [Select]
private void iterate(EA.Element element, XmlSchema schema, bool isRoot)
{
    ............
    foreach (EA.Connector con in element.Connectors)
    {
        if (con.Type.ToString() == EA_Element.Aggregation.ToString() &&
            con.Stereotype.ToString() == CCTS_Types.ASBIE.ToString())
        {
            .........
            iterate(client, schema, false);
        }
    }
}  


After :

Code: [Select]
private void iterate(EA.Element element, XmlSchema schema, bool isRoot)
{
    .........
    for (short a = 0; a < element.Connectors.Count; a++)
    {
        EA.Connector con = (EA.Connector)element.Connectors.GetAt(a);
        if (con.Type.ToString() == EA_Element.Aggregation.ToString() &&
            con.Stereotype.ToString() == CCTS_Types.ASBIE.ToString())
        {
            .........
            iterate(client, schema, false);
        }
    }
}  

Almost forget to mention, I'm using C# with .Net 2005 and Win XP sp 2.
This error occurs on version 7.0 (i forget which build) and version 7.1 (build 832 and 833) - I tested on these version, there might be a chance in other version.

Thanks in advance.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Recursive method inside foreach failed
« Reply #1 on: October 31, 2008, 05:45:12 pm »
Just curious...

Where does client come from?

If it comes from the connector Client, you'll have infinite recursion.

Kezia

  • EA User
  • **
  • Posts: 33
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Recursive method inside foreach failed
« Reply #2 on: November 03, 2008, 09:49:23 pm »
Of course not infinite loop. I have added some codes to avoid that. I only need the connector that comes out from current element for recursive, NOT all connector.

I think, it's not important to write down my code here, coz the actual problem is why recursive method works inside "for" loop but not inside "foreach" loop on EA collection.

If it's infinite loop, how come it works on debug mode? and also in debug build?
And another people also has the same problem with me (as I stated the link in previous post), and the also the exactly same solution??

And again, if it's infinite loop, it would be also infinite loop on "for", not only on "foreach".