为什么我收到此StackOverflowException?

Fro*_*mer 6 c# stack-overflow tweetsharp

我不确定这里发生了什么,它使用Twitter API进行身份验证就好了.

但当它到达应该发布到Twitter的点时,它会抛出一个StackOverflowException说:

An unhandled exception of type 'System.StackOverflowException' 
occurred in mscorlib.dll

我很困惑.下面的代码是导致异常并最终导致异常的原因.

    void StartValidation()
    {
        Console.Clear();
        //Start Status thread
        var status = TextAndUi.GetStatisThread();
        status.Start("Validating");

        //Check for Messages
        var tweetAndSenderData = Imap.GetUnreadMessageAndSender();

        if (tweetAndSenderData != null)
        {
            //Authurize connection and app
            var authenticate = new Authenticate();
            var tweetApp = authenticate.CreateClient();

            //End thread
            status.Abort();
            Console.WriteLine("Validated!");
            Console.Clear();

            //Post tweets
            PostContent("test", tweetApp);

            //Delete messages
            Imap.DeleteMessages();
        }
        else
        {
            //End thread
            status.Abort();
            TextAndUi.ShowSomethingToTheUser("The box is empty, or TTT could not secure a connection", true);
        }
    }

    void PostContent(string myTweet, TwitterService tweetApp)
    {
        if (TextAndUi.MessageIsSuitableLength(myTweet))
        {
                PostTweet(tweetApp, myTweet);
        }
    }

    void PostTweet(TwitterService tweetApp, string tweet )
    {
        var tweetOptions = new SendTweetOptions() {Status = tweet};

        tweetApp.SendTweet(tweetOptions); /*The line that throws the exception*
    }
Run Code Online (Sandbox Code Playgroud)

正在使用的库是TweetSharp.

编辑:添加了CallStack数据

mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b bytes   
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 bytes  
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes   
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes   mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   

Jas*_*ams 1

堆栈溢出通常意味着您的程序已进入递归调用彼此的方法的无限循环。

最可能的原因是:

  • 您正在使用的库中有一个错误。由于您的代码很简单,因此您的库的作者似乎不太可能在测试中错过这样的错误,但您可能传入的参数值会导致问题。您可以尝试使用不同的参数进行一些调用,看看它是否与您的调用方式有关。

  • 您已编写但未发布的某些代码正在导致对其自身的递归调用。这在以下情况下可能非常明显:void a() { a(); }但它也可能非常微妙 - 如果您尝试发布推文作为对事件的反应,则发送推文很可能会导致事件再次引发,从而导致无限反馈回路。检查这一点的最简单方法是在 SendTweet() 行上放置一个断点,并检查该断点是否被击中多次。如果是,那么您需要消除可重入调用 - 这可以通过在进行调用之前取消注册事件处理程序(并在之后重新注册)或使用变量来抑制对调用的处理来完成,例如这:

    bool mSuppressTweets;
    
    void PostTweet(TwitterService tweetApp, string tweet )
    {
        if (mSuppressTweets)
            return;
    
        var tweetOptions = new SendTweetOptions() {Status = tweet};
    
        mSuppressTweets = true;
        tweetApp.SendTweet(tweetOptions);
        mSuppressTweets = false;
    }
    
    Run Code Online (Sandbox Code Playgroud)

如果这些都没有帮助,那么尝试隔离问题。创建一个新的“hello world”项目,仅发送一条推文,不发送任何其他内容。然后你就会知道你有一个有效的推文解决方案,并且你可以将工作代码迁移到你的原始应用程序中,看看它是否在那里工作。如果它仍然不起作用,您就知道您的应用程序所做的事情与您的“hello world”测试不同。