Why am I receiving this StackOverflowException?
Asked Answered
B

3

6

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

But when it gets to the point where it should post to twitter, it throws a StackOverflowException that says:

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

I'm pretty baffled. The code below is what leads up to and ultimately causes the 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 being 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.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   
Boulevard answered 5/9, 2013 at 15:53 Comment(7)
Post SendTweet methodPhanerozoic
If you're able to debug this in Visual Studio, open the Call Stack window (Ctrl + C, D) and you the answer should be right there. You probably have method a() calling method b(), that calls method a() again (either directly or indirectly through one or more other methods).Reta
@RichardEv Thanks for that! I'll be honest though, there's not much I can gather from it. Can you get anything out of it?Boulevard
What makes you think that the code you've posted causes the StackOverflowException? Have you tried setting a breakpoint in your code and step through until the exception happens?Reta
Suspect that the exception is happening within TweetSharp (github.com/danielcrenna/tweetsharp ?) It might be worth pulling down the source code to TweetSharp, and creating a debug build to run against...Reta
Yes, The point I've indicated was the last line to be consumed before throwing the exception. I'll look into the source code.Boulevard
@RichardEv: stepping through code until a StackOverflowException occurs could take quite a while! Breaking on the exception is likely to be easier ;-)Janenejanenna
N
3

I had the same problem and was able to resolve it. For me, the problem occurred because on Twitter, I had only given my application read-access. To write a tweet from your code, your application must be registered as read/write on twitter.

To do this, I had to first revoke the application from the twitter account I was using it with. Then, I changed the application on dev.twitter so that it was read/write. I then generated a new access token and was able to send tweets from my code.

Hope that helps.

Nolly answered 2/12, 2013 at 14:6 Comment(0)
J
1

A Stack Overflow usually means that your program has gone into an infinite loop of methods that call each other recursively.

The most likely causes for this are:

  • There is a bug in the library you are using. As your code is simple, it seems unlikely that the author of your library would have missed such a bug in their testing, but it is possible that you are passing in a parameter value that causes a problem. You could try making some calls with different parameters to see if it is something specific about the way you're calling it.

  • Some code you have written but not posted is causing a recursive call to itself. This can be quite obvious in cases like: void a() { a(); } but it can also be very subtle - if you attempt to post a tweet as a reaction to an event, it is quite possible that sending the tweet causes the event to be raised again, causing an infinite feedback loop. The easiest way to check this is to put a breakpoint on your SendTweet() line and check if the breakpoint is hit more than once. If it is, then you need to eliminate the re-entrant call - this can be done by unregistering your event handler before making the call (and re-registering it again afterwards) or using a variable to suppress your processing of the call, like 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 neither of these things helps, then try to isolate the problem. Create a new 'hello world' project that just sends a tweet and nothing else. Then you will know you have a working solution to tweeting, and you can migrate the working code into your original app to see if it works there. If it still doesn't work, you know it's something that your app is doing differently from your 'hello world' test.

Jarred answered 5/9, 2013 at 20:14 Comment(0)
S
1

I made an account to answer this, and I don't have enough "reputation" to comment, but Greg B's answer is the correct one.

If your app does not have the required permissions to post tweets, calling the SendTweet(SendTweetOptions) method will result in a StackOverflowException.

I just fixed this same issue by logging into my account at https://dev.twitter.com/ and updating my app permissions under settings and then re-authenticating my account (ie. generating a new token).

Sperling answered 6/1, 2014 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.