递归函数 C# 中的 StackOverFlow [英] StackOverFlow in recursive function C#

查看:74
本文介绍了递归函数 C# 中的 StackOverFlow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,但是有一个递归函数,我无法优化它,这就是为什么我总是有 StackOverFlow 异常.你能帮我优化一下吗?我使用这个 void 来获取所有信息或在发现一些错误时重试.

I have this code, but there is an recursive function and i can't optimize it, that's why i always have StackOverFlow exception. Can you help me to optimize it? I use this void to get all info or to try again if some errors are found.

 public void Parse(byte[] data)
    {
        Socket skt = new Socket(SocketType.Dgram, ProtocolType.Udp);
        skt.ReceiveTimeout = 1000;

        skt.Connect(Encryptor.GetNextIP(), port);
        skt.Send(data);
        try
        {
         //a lot of actions to get the opcode
            switch (opCode)
            {
                case 0x01:
                    p = new Packet(opCode);
                    p.WriteArray(Encryptor.ClientData(answer, false));
                    Regex rgx = new Regex("\"(?<Name>.+?):");
                    string part = p.ReadAString();
                    string res;
                    if (part[part.Length - 1] != '}') part += '}';
                    if (rgx.Match(part).Groups["Name"].Value == "")
                    {
                        p = null;
                        Parse(data);
                        break;
                    }
                    else
                    { res = "ID=" + rgx.Match(part).Groups["Name"].Value; }
                    ParseInfo(rgx.Match(part).Groups["Name"].Value);
                    p = null;
                    Encryptor = null;
                    break;
                default: string ans = Encoding.ASCII.GetString(recv.Take(bytes).ToArray());
                    if (ans.Contains("EndOfStream") || ans.Contains("Begin"))
                    {
                        Failed();
                        Encryptor = null;
                        break;
                    }
                    else
                    {
                        p = null;
                        Parse(data);
                    }
                    break;
            }
        }
        catch
        {
            skt.Close();
            skt.Dispose();
            GC.Collect();
            Parse(data);
        }
    }

提前致谢.

推荐答案

所有递归函数都必须具有并达到终止条件才能回滚堆栈.您忽略了 opCode 值的导出方式,但如果该值始终为 0x01,并且正则表达式的计算结果为 true,则函数将永远不会达到终止条件.默认情况下相同,从未看到开始或 EOS.

All recursive functions must have and reach a terminating condition to rollback the stack. You left out how opCode value is derived, but if that one is ALWAYS 0x01 with regex evaluating to true, then function will never reach terminating condition. Same for default case and never seeing begin or EOS.

另一种看待它的方式是,对于每次递归调用,它必须使您更接近结束.

Another way of looking at it is that with every recursive call, it must take you one step closer to the end.

所以检查你的代码逻辑,看看为什么你永远不会达到终止条件.

So examine your code logic to see why you never reach terminating condition.

此外,假设内存错误稍后再次开始递归的空白捕获也可能有问题......如果套接字返回一些通用异常(无连接),那么你将永远递归(直到堆栈被炸毁)

also, blank catch that assumes memory fault that later starts recursion again is also possibly at fault... if socket returns some generic exception (no connectivity) then you'll recurse forever (until stack is blown)

这篇关于递归函数 C# 中的 StackOverFlow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆