locked
XmlTextReader - StackOverflow - Infinite loop RRS feed

  • Question

  • I get a StackOverflowException, caused by the while loop (my guess). Howcome this code enters an infinite loop? There are two variants, one with a counter (Variant 2), that should exit the loop, and one without the counter (Variant 1).

    It seems to be very hard to debug this part of my program, because when I insert a breakpoint, then there will be displayed this informantion: "Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.".

    What can I do to get this code up and going.

    Regards Gunnar

    Variant 1:

     private String url;
     private XmlTextReader reader;


            public ReadXMLSMS(String xmlurl)
            {
                url = xmlurl;
                reader = new XmlTextReader(url);
                int i = 0;
                while (reader.Read())
                {
                    if (reader.Name.Equals("number"))
                        phonenumber = reader.ReadString().Trim();
                    if (reader.Name.Equals("message"))
                        message = reader.ReadString().Trim();
                    if (reader.Name.Equals("errorlevel"))
                        errorlevel = reader.ReadString().Trim();
                }
            }

     

    Variant 2:


     private String url;
     private XmlTextReader reader;


            public ReadXMLSMS(String xmlurl)
            {
                url = xmlurl;
                reader = new XmlTextReader(url);
                int i = 0;
                while (reader.Read() || i > 10)
                {
                    if (reader.Name.Equals("number"))
                        phonenumber = reader.ReadString().Trim();
                    if (reader.Name.Equals("message"))
                        message = reader.ReadString().Trim();
                    if (reader.Name.Equals("errorlevel"))
                        errorlevel = reader.ReadString().Trim();
                    i++;
                }
            }

    Monday, December 26, 2005 10:45 AM

All replies

  • First variant looks like correct, but the second definitely not. If you want to limit the number of processed entities, replace loop condition with the following (otherwise it will go into infinite loop if your xml contains more than 10 entities):

    while (reader.Read() && i<=10)

    Monday, December 26, 2005 12:36 PM
  • Hi,

    Can you provide me the XML which you are using? 

    In Second variant there is a problem.

    If you have more than 10 nodes in your xml then in your while condition as you have put "||" , though the EOF and no more nodes then also always second condition will be true and it will be infinite loop so after some time when your memory is full, it will give you that error.

    So you should change your condition and be careful when you are using "Oring" condition in while loop because normally if you made any simple mistake it will leads to infinite loop so again check your requirement and change your while condition in second snippet. I am not able to understand the condition "i>10" - may be it will be by mistake and you should use && in some cases so you can limit your iterations. But finally it depends on your requirement.

    HTH,

     

    Monday, December 26, 2005 1:20 PM