为什么我收到此StackOverflowException?

时间:2013-09-05 15:53:30

标签: 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*
    }

正在使用的库是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   

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,并且能够解决它。对我来说,问题出现是因为在Twitter上,我只给了我的应用程序读取权限。要从您的代码中编写推文,您的应用程序必须在Twitter上注册为读/写。

要做到这一点,我必须首先从我正在使用它的Twitter帐户撤销该应用程序。然后,我在dev.twitter上更改了应用程序,以便它可以读/写。然后,我生成了一个新的访问令牌,并能够从我的代码中发送推文。

希望有所帮助。

答案 1 :(得分:1)

Stack Overflow通常意味着你的程序进入了一个无限循环的方法,它们以递归方式相互调用。

最可能的原因是:

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

  • 您编写但未发布的某些代码会导致对自身的递归调用。这在以下情况下非常明显: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”测试有所不同。

答案 2 :(得分:1)

我做了一个帐户来回答这个问题,我没有足够的“声誉”来评论,但格雷格B的回答是正确的。

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

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