Results 1 to 7 of 7

Thread: [RESOLVED] StackOverflowException in Richtextbox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Resolved [RESOLVED] StackOverflowException in Richtextbox?

    I'm trying to read the lines of a richtextbox into a string array but no matter how hard i try i keep getting a stackoverflow exception. This is what I currently have (Notes is the name of my Richtextbox):

    Code:
    Dim newarr() As String = Notes.Lines
    Before that I tried:

    Code:
    Dim newarr() As String = Notes.Text.Split(Environment.NewLine)
    And also:

    Code:
    Dim newarr() As String = Notes.Text.Split(vbCrlf)
    Nothing I try is working!! Where am I going wrong???

  2. #2

    Re: StackOverflowException in Richtextbox?

    What is your entire code? There could be more going on in other parts of the program. How many lines are in the RTB?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: StackOverflowException in Richtextbox?

    When you get the error, take a look at the stack trace. The most common cause for that particular exception is recursion. There isn't any recursion in what you are showing us, but the stack trace would tell you. If you see a massive number of nested, yet repetitive, calls, you have recursion. Other than that, how big is the data in your RTB?
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Re: StackOverflowException in Richtextbox?

    Quote Originally Posted by formlesstree4 View Post
    What is your entire code? There could be more going on in other parts of the program. How many lines are in the RTB?
    Thats the thing- before I click the button that runs the code I go to the RTB and enter two or three lines of gibberish just to test the code. I'm away from my pc right now so I'll post the rest of the code later. But yeah... Even with the lines of gibberish I still get an error. Also, what is recursion?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: StackOverflowException in Richtextbox?

    Recursion is when a function calls itself. It could be A calling A, or A calling B which calls A, or so forth. Each time a function is called, it gets a piece of the stack to hold all its local variables and some other things. Therefore, when A calls A, the second calling gets a new piece of the stack while the caller still has a piece of the stack. This is fine, as long as the loop returns....eventually. Infinite recursion is when A calls A which calls A which calls A....and so forth without any of them ever returning. Pretty quickly, the stack grows larger than the OS has memory for and that's the end of your program. It's the most common scenario for the error you are getting, and easy to check for....but not necessarily easy to solve.

    One of the common ways that this happens in Windows programming is that one event does something that causes another event to be raised, which does something that causes the first event to be raised....again, and so on until the system runs out of memory.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Re: StackOverflowException in Richtextbox?

    Okay, so I got it figured out. I had this line in the code:

    Code:
      If InStr(PrepWordList.Text, replaced) Or previousquestion = question Or InStr(CommonWord.Text, replaced) Then
                    QGen()
                Else
                    q.Visible = True
                    q.Text = question
                    correctanswer.Text = replaced
                    previousquestion = question
    
                End If
    It worked before. Until I realized instead of checking if the word was in the prepwordlist it was checking for letters. Obviously the chosen letter is going to be in a list of words (it's a long list of prepositions). Anyway, it's working. Thanks u guys

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: StackOverflowException in Richtextbox?

    A couple suggestions for future use:
    - Use .Contains instead of InStr: PrepWordList.Text.Contains(replaced), CommonWord.Text.Contains(replaced)
    - Use OrElse instead of Or
    - Please mark your thread resolved if you have no more questions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width