Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
OK, how can I improve this code to stop the stackoverflow from happening here:
VB.NET
Public Sub CheckForReplacementText() function



The code is supposed to do Grammar checking when you press F1 to run the program. But for this to happen, you need to copy the dictionary I provided from Google in order for it to work.

The error is pointed from here:

VB.NET
foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)



The whole code is here:

VB.NET
    Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
        If e.KeyCode = Keys.F1 Then

            CheckForReplacementText()
        End If
    End Sub

    Public Sub ReplaceMenu_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ReplaceMenu.ItemClicked


        For Each replacement In replacements(checkWord)

            replacement = e.ClickedItem.Text
            kamau = replacement

        Next


    End Sub
End Class


Press F1 to run the program. My dictionary can be found here:

https://docs.google.com/document/d/1eTZaIUh7Q9zkNzjZQ-lPV9yxS5eSlNs5ht2-Pl-LS14/edit?usp=sharing
Posted
Updated 25-May-16 22:35pm
v7
Comments
F-ES Sitecore 17-Dec-15 8:29am    
Stack overflow errors happen when your code gets into a infinite call loop, ie something like this

public void DoSomething()
{
DoSomething();
}

Use the debugger to step through your code to find out what is looping, that's the first stage. Once you know the solution might become obvious, if not update your question to say what is looping.
[no name] 17-Dec-15 9:05am    
The section that is looping is the Public Sub CheckForReplacementText() function from here:

foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)

You have two calls back to CheckForReplacementText in your CheckForReplacementText method. Also, there is no bailout to stop this endless process, so yeah, your code is going to throw a stack overflow error.

Why are you calling CheckForReplacementText from inside the method?

Your code doesn't make any sense at all. I have no idea what it's supposed to be doing. The CheckForReplacementText method is doing a ton more stuff than it's name suggests it should be doing.

Also, the With statement you have in there doesn't need to exist. Remove the With/End With and just leave the rest of the statement there, IF that's what your logic is supposed to be doing.
 
Share this answer
 
v2
Comments
[no name] 17-Dec-15 9:50am    
Yes, the code is supposed to be replacing words with suggestions from the second column from the dictionary. It uses a contextmenu to replace bad words. How can I programmingly deal with this problem after removing With/End With?
Dave Kreskowiak 17-Dec-15 9:54am    
The big problem is why is the CheckForReplacementText method calling iteself?
CHill60 17-Dec-15 9:58am    
Because the OP will not listen to advice given! I pointed out the recursion on one of their earlier posts. Basically I think they are getting their homework done one QA post at a time without understanding any of the answers
Dave Kreskowiak 17-Dec-15 10:11am    
Figures...
[no name] 19-Dec-15 12:20pm    
Can you help me with this?
Stack overflow condition is maybe not the easiest for debugging, but it's one of the easiest. You cannot ask a question every time it happens but really want to learn how to sort out such problems all by yourself.

Here are some basic ideas. If stack overflow happens as a result of some bug, it's nearly always happens due to incorrect recursion or mutual recursion:
https://en.wikipedia.org/wiki/Recursion[^],
https://en.wikipedia.org/wiki/Recursion_(computer_science)[^],
https://en.wikipedia.org/wiki/Mutual_recursion[^],
https://en.wikipedia.org/wiki/Recursive_function[^].

If you know which of your recursive function written on purpose is a source of a problem, you can just debug it in a usual way. But what if you don't know where the problem is?

First, when you observer this exception, put a break point, at first, at the very first statement of the method shown where the exception happens under the debugger (in Visual Studio, you may want to adjust Debug/Exceptions settings). Restart the application and reproduce the condition. When the execution stops at this point, continue execution to see how this point is reached again and again.

Now, this is the key: use the debugger window "Call stack" to see where the call comes from each time. Using this information, move or add the breakpoint to the point in code closer to the source of this problem. This way, you methodically pin-point the source of the problem in few such iteration steps.

—SA
 
Share this answer
 
Comments
[no name] 17-Dec-15 11:48am    
I studied the problem thoroughly. My only problem is the ingenuity to reproduce another function to ease the workload.
Sergey Alexandrovich Kryukov 17-Dec-15 13:26pm    
Good. That could be a problem, if the stack overflow only happens due to non-deterministic behavior. Are you sure you cannot reproduce the same very behavior after each restart? If so, you should think of possible reasons for non-deterministic behavior, but perhaps you should just do all the observation very accurately.
—SA
[no name] 17-Dec-15 12:19pm    
If we don't solve this problem as men, then the subsequent users will find the question unanswered, and then they will ask you the same question over, and over again.
Sergey Alexandrovich Kryukov 17-Dec-15 13:28pm    
The similar questions have been asked too many times before, to worry about it too much. Your specific problem might be only yours, not interesting to others, only the general problem is interesting.

Now, why would you address this comment to me, "as men", and so on? Just curious...

—SA
[no name] 17-Dec-15 13:51pm    
Right now am too busy for cheat chat.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900