为什么我会收到这个StackOverflowException异常?

6
我不确定这里发生了什么,它能够很好地使用 Twitter API 进行身份验证。
但是当它应该发布推文时,会抛出一个 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*
    }

使用的库是TweetSharp。

编辑:添加了CallStack数据

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

7
如果你能在Visual Studio中调试此代码,请打开“调用堆栈”窗口(Ctrl + CD),答案应该就在那里。很可能是方法a()调用了方法b(),然后再次调用了方法a()(直接或通过一个或多个其他方法间接地调用)。 - Richard Ev
@RichardEv 谢谢你!不过说实话,我自己从中没能获得太多信息。你能从中得到什么吗? - Frostytheswimmer
你认为你发布的代码是导致“StackOverflowException”的原因吗?你尝试在代码中设置断点并逐步执行,直到出现异常吗? - Richard Ev
怀疑异常发生在TweetSharp(https://github.com/danielcrenna/tweetsharp?)中。值得拉下TweetSharp的源代码,并创建一个调试版本来运行... - Richard Ev
是的,我指出的那个点是在抛出异常之前被消耗的最后一行。我会查看源代码。 - Frostytheswimmer
显示剩余2条评论
3个回答

3
我曾经遇到同样的问题并解决了它。对我来说,这个问题是因为在Twitter上,我只给了我的应用程序读取权限。要从您的代码中编写推文,您的应用程序必须在Twitter上注册为可读/写。
为了做到这一点,我首先必须从我正在使用它的Twitter帐户中撤销该应用程序。然后,我更改了dev.twitter上的应用程序,使其可读/写。然后,我生成了一个新的访问令牌,并能够从我的代码发送推文。
希望这有所帮助。

1

栈溢出通常意味着您的程序进入了一个相互递归调用的方法的无限循环。

最可能的原因是:

  • 你使用的库中存在一个bug。由于你的代码很简单,所以似乎不太可能作者在测试中会忽略这样一个bug,但是有可能是你传递了一个导致问题的参数值。你可以尝试使用不同的参数进行一些调用,看看是否与你调用的方式有关。

  • 你编写但未发布的某些代码会导致递归调用本身。在某些情况下这可能非常明显,例如: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;
    }
    
如果以上两种方法都没有帮助,那么尝试隔离问题。创建一个新的“hello world”项目,只发送一条推文,然后您就会知道您已经有了一个可行的解决方案来发送推文,并且您可以将工作代码迁移到原始应用程序中以查看是否有效。如果仍然无法正常工作,则说明您的应用程序与您的“hello world”测试有所不同。

1

我注册了一个账户来回答这个问题,但是我的“声望”还不够高,无法进行评论,但 Greg B 的回答是正确的。

如果您的应用程序没有所需的推文发布权限,则调用 SendTweet(SendTweetOptions)方法将导致 StackOverflowException。

我通过登录https://dev.twitter.com/并在设置下更新我的应用程序权限,然后重新验证我的帐户(即生成新令牌)来解决了同样的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接