Why am I getting this StackOverflowException?

I'm not sure what is going on here, it authenticates with the Twitter API just fine.

But when it gets to the point where it should tweet, it throws a StackOverflowException that states:

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

I'm pretty puzzled. The code below is what causes and ultimately throws an exception.

  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* } 

The library used is TweetSharp.

Edit: Added CallStack data

  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.ExecutionConread.onecreadtextontext .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 
+6
source share
3 answers

I had the same problem and I was able to solve it. For me, the problem arose because on Twitter I only got access to read the application. To write a tweet from your code, your application must be registered as read / write on Twitter.

To do this, I had to first cancel the application from the Twitter account with which I use it. Then I changed the application to dev.twitter so that it was read / written. Then I generated a new access token and was able to send tweets from my code.

Hope this helps.

+3
source

Stack overflow usually means that your program has gone into an endless loop of methods that recursively call each other.

The most likely causes for this are:

  • There is an error in the library. Since your code is simple, it seems unlikely that the author of your library would have missed such an error when testing them, but it is possible that you are passing a parameter value that causes the problem. You can try to make several calls with different parameters to find out if this is something specific in how you call it.

  • Some code that you wrote but didn’t send causes a recursive call for yourself. This can be quite obvious in such cases as: void a() { a(); } void a() { a(); } , but it can also be very subtle - if you try to post a tweet in response to an event, it is possible that sending a tweet causes the event to rise again, causing an endless feedback loop. The easiest way to check this is to set a breakpoint in your SendTweet () line and check if the breakpoint succeeded more than once. If so, then you need to eliminate the callback - this can be done by unregistering your event handler before making the call (and registering it again after that) or using a variable to suppress call processing, for example this:

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

If none of these things help, try isolating the problem. Create a new project "hello world" that just sends a tweet and nothing more. Then you will find out that you have a working tweet solution, and you can transfer the working code to the source application to see if it works there. If it still does not work, you know that this application is different from your test "hello world".

+1
source

I made an account to respond to this, and I don't have enough “reputation” for comments, but Greg B.'s answer is correct.

If your application does not have the necessary permissions to send tweets, calling SendTweet (SendTweetOptions) will throw a StackOverflowException.

I just fixed this problem by logging into my account at https://dev.twitter.com/ and updating the permissions for my application in the settings, and then re-authenticating mine (i.e. creating a new token).

+1
source

Source: https://habr.com/ru/post/953256/


All Articles