2013-09-05 2 views
6

나는 여기서 무슨 일이 일어나고 있는지 잘 모르겠다. 트위터 API로 인증한다.왜이 StackOverflowException을 수신합니까?

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

내가 꽤 당황 해요 : 그것은 트위터에 게시해야하는 지점에 도달 할 때

는하지만, 그것은라는 StackOverflowException 발생합니다. 아래의 코드는 예외를 초래하고 궁극적으로 예외의 원인이되는 것입니다.

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입니다.

편집 : 추가 된 호출 스택 데이터

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
+0

'SendTweet' 메서드 게시 –

+7

Visual Studio에서 이것을 디버깅 할 수 있다면 * 호출 스택 * 창 (** Ctrl ** + ** C **, ** D **)을 열고 대답은 바로 거기 있어야합니다. 메소드'a()'를 (직접 또는 간접적으로 하나 이상의 다른 메소드를 통해) 다시 호출하는 메소드'b()'를 호출하는 메소드'a()'를 가지고있을 것이다. –

+0

@ 리차드 에브 감사합니다! 나는 정직 할 것이다, 거기에서 많이 모을 수 없다. 당신은 그것에서 무엇이든을 얻을 수 있는가? – Frostytheswimmer

답변

1

스택 오버플로는 일반적으로 프로그램이 반복적으로 서로를 부르는 방법의 무한 루프에 간 것을 의미한다. 이것에 대한

가장 큰 원인은 다음과 같습니다

  • 사용중인 라이브러리에있는 버그가 있습니다. 코드가 간단하기 때문에 라이브러리 작성자가 테스트에서 그러한 버그를 놓친 것 같지는 않지만 문제의 원인이되는 매개 변수 값을 전달할 가능성이 있습니다. 다른 매개 변수를 사용하여 전화를 걸면 전화하는 방식과 관련하여 특정 통화인지 확인할 수 있습니다.

  • 작성했지만 게시하지 않은 코드로 인해 재귀 호출이 발생합니다. 다음과 같은 경우에는 매우 명확 할 수 있습니다.하지만 아주 미묘 할 수도 있습니다. 이벤트에 대한 반응으로 트윗을 게시하려고하면 트윗을 보내면 다시 이벤트가 발생하여 무한 피드백 루프. 이를 확인하는 가장 쉬운 방법은 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; 
    } 
    

이런 것들 중 어느 것도, 다음 문제를 분리하려고하는 데 도움합니다. 짹짹 만 보내고 다른 것은 보내지 않는 새로운 '안녕하세요 세상'프로젝트를 만드십시오. 그런 다음 tweeting에 대한 실질적인 솔루션을 알고 작업 코드가 원래의 응용 프로그램으로 마이그레이션하여 해당 코드가 작동하는지 확인할 수 있습니다. 그래도 작동하지 않는다면 앱이 '안녕하세요 세상'테스트와 다른 방식으로 수행되고 있음을 알게됩니다.

3

동일한 문제가있어서 해결할 수있었습니다. 나에게있어 문제는 트위터에서 내 응용 프로그램에 읽기 액세스 만 허용했기 때문에 발생했다. 코드에서 트윗을 작성하려면 응용 프로그램을 트위터에 읽기/쓰기로 등록해야합니다.

이렇게하려면 먼저 내가 사용하고 있던 트위터 계정에서 응용 프로그램을 취소해야했습니다. 그런 다음 dev.twitter의 응용 프로그램을 읽기/쓰기로 변경했습니다. 그런 다음 새 액세스 토큰을 생성하고 내 코드에서 트윗을 보낼 수있었습니다.

희망이 있습니다.

1

나는 이것을 대답하기위한 계정을 만들었으며 논평하기에 충분한 "명성"이 없지만 그렉 B의 대답은 정확한 것입니다.

앱에 트윗 올리기 권한이없는 경우 SendTweet (SendTweetOptions) 메소드를 호출하면 StackOverflowException이 발생합니다.

내 계정에 https://dev.twitter.com/으로 로그인하고 설정에서 내 앱 권한을 업데이트 한 다음 내 계정을 다시 인증 (즉, 새 토큰 생성)하여이 문제를 해결했습니다.

관련 문제