【问题标题】:Why am I receiving this StackOverflowException?为什么我会收到此 StackOverflowException?
【发布时间】:2013-09-05 15:53:30
【问题描述】:

我不确定这里发生了什么,它可以通过 Twitter API 进行身份验证。

但是当它到达应该发布到推特的地步时,它会抛出一个StackOverflowException,上面写着:

“System.StackOverflowException”类型的未处理异常
发生在 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(对象状态)+ 0x6f 字节
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0xa7 字节 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading .ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0x16 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态) + 0x41 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 字节

【问题讨论】:

  • 发布SendTweet方法
  • 如果您能够在 Visual Studio 中调试它,请打开 Call Stack 窗口(Ctrl + C , D) 你的答案应该就在那里。您可能有方法a() 调用方法b(),再次调用方法a()(直接或间接通过一种或多种其他方法)。
  • @RichardEv 谢谢!不过老实说,没有太多可以从中收集到。你能从中得到什么吗?
  • 是什么让您认为您发布的代码导致了StackOverflowException?您是否尝试过在代码中设置断点并单步执行直到发生异常?
  • 怀疑异常发生在 TweetSharp (github.com/danielcrenna/tweetsharp ?) 可能值得将源代码拉到 TweetSharp,并创建一个调试版本来运行...

标签: c# stack-overflow tweetsharp


【解决方案1】:

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

为此,我必须首先从我使用它的 twitter 帐户中撤消该应用程序。然后,我更改了 dev.twitter 上的应用程序,使其可以读/写。然后我生成了一个新的访问令牌,并能够从我的代码发送推文。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

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

    最可能的原因是:

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

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

    【讨论】:

      【解决方案3】:

      我注册了一个账号来回答这个问题,我没有足够的“声誉”来发表评论,但 Greg B 的回答是正确的。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-14
        • 2020-02-24
        • 2013-12-09
        • 2018-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多