Results 1 to 12 of 12
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Losing one's mind tracking down a VB bug

    If any of you wish to see what is involved in losing one's mind while isolating a VB bug, take a look at:

    http://www.standards.com/temporary/OutOfStackSpace.html

    You would need VB 6 to actually run the code, but you could still look at the source for the .bas and .cls files using VBA, and you could look at the .frm with, say, Wordpad.

    A really punishing bug to locate.

  2. #2
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Losing one's mind tracking down a VB bug

    Thanks Howard. What little mind I had left is now completely gone. Indeed this is a vicious bug but I'm not sure that using a class is a bug in VB.

    I used the code in the NoFunction folder.

    Firstly, I converted the code to .NET to see if it made any difference and it didn't. Same error.

    I use a QuickSort function myself so I added a modified version of my function to the project and it works fine. I've run the program with values from 100 to 5,000,000 with no Stack Overflows.

    I added some code to send some output about each call to QuickSortDouble to a log file and ran the program with the original code and then with my code. This showed that using the orignal code the QuickSortDouble function was called approximately 4 times as many times than with my function. A lot of the calls had a max parameter one less than the min parameter. Whether this is the cause of the Stack Overflow I'm not sure.

    As you rightly point out, this is a b*tch of a problem to find and solve. Recursive procedures are always magical in some ways and VB doesn't really give you much in the way of debugging tools when it comes to errors such as this. Mind you, .NET isn't much better.

    I've attached a ZIP file containing the code I modified for your information.

    I hope in some small way it comforts you to know that there is another being on the planet who has lost as many grey cells as you over this problem.

    Regards,

    Kevin Bell

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    Well, if misery loves company, now I do not feel so alone.

    As I pointed out, the particular quicksort algorithm leaves much to be desired and uses deeper recursion than any of the others.
    Whatever is going on does seem to cause a problem with the class. This should not happen, so I would declare that aspect to be a bug in VB.

    My gut feeling, and I do have a big gut, is that the problem actually lies in the VB error handling with native mode code in a class, rather than in a module.
    Note that other than the On Error GoTo Someplace, I removed all the On Error goto 0 and On Error Goto -1.

    I did retain the 2 Err.Clear in pDoQuiick and the .Clear in the two subs that process errors. Heck, if those are causing the problem, then ...!!

    I have also seen the error with VB .NET.

    The real problem in finding this problem is that it is difficult to continue after the Out Of Stack Error, especially in VB .NET.
    For ordinary errors, I could disable error handling, fix the first error, then continue. Alas, in this case, the stack error is both expected and desired, but something breaks afterwards for the RS/pRS algorithm. However, this error does not occur with p-code .exe or in design mode, so ...!

    Note that algorithms, S7.1, S7.1= and S7.5 can also be easily coereced into causing an out of stack error, but they do not seem to cause the subsequent Error 91 problem.

    The calls with the max being 1 less than the min are just symptomatic of a poor quicksort algorithm.

    Note that I have code for another quicksort algorithm that is even worse, i.e., it blows the program away without even an error message.

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    I don't know whether this will help solve the problem I raised in this thread, but moving the code to VB .NET reveals a problem with the use of the Rnd function and the error handler

    In VB .NET, not only do I need to replace the Rnd function in the RS and pRS algorithms with the native VB .NET Random, I also need to use Try/Catch in the calling sub, instead of On Error.

    Note that I use Rnd in some of the other subs with no problem.

    So it would appear the the particular implementation of quicksort in RS and pRS algorithms is causing recursion so deep that the On Error mechanism is getting munged by some use of Rnd, at least in VB .NET.

    I replaced all the On Error GoTo statements with Try/Catch and added the Random, then stack error was trapped.

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    False alarm!

    Using Rand and Try/Catch does not eliminate the error, just postpones its appearance.

    Private CountMe As Long = 0
    Private min As Long = 0
    Private max As Long = 999
    'Private rand As New Random
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Try
    TestRecursion()
    Catch ex As Exception
    Debug.WriteLine(ex, "btnStart")
    End Try
    End Sub

    Private Sub TestRecursion()
    Dim x As Double
    ' Rnd or Int here...
    x = Int((max - min + 1) * Rnd() + min)
    'x = rand.Next(min, max)
    CountMe = CountMe + 1
    With lblRecursion
    .Text = CStr(CountMe) & ": " & x.ToString
    .Refresh()
    End With
    TestRecursion()
    End Sub

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    I thought that I had posted this yesterday, but I don't see it, so here it is again.
    This is a corrected version.
    --------------------------------------
    Eureka!

    The problem has nothing to do with the sorting routines.
    The poor sorting algorithm does make stack overflow occur sooner rather than
    later, but it has nothing to do with the VB.NET bug preventing my code from
    trapping stack overflow.

    I created a clean VB .NET project and put 1 label and 2 button controls on
    the form.

    I then ran the code below.
    Stack overflow occurred in due course and the Try block caught it.

    However, if either, or both of the following is done, the Try block does not
    catch the stack overflow:

    1. Uncomment the mechanism with Rnd. Problem does not occur using randy.

    2. Uncomment the With ... End With that updates the lblRecursion textbox.

    Doing either, or both, prevents Try from trapping the stack overflow.

    That's one, to be polite, darn bug in VB .NET.

    '----------------------------------------------------
    <pre> Private CountMe As Integer
    Private Const min As Integer = 0
    Private Const max As Integer = 999
    'Private randy as New Random


    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnStart.Click
    CountMe = 0
    Try
    TestRecursion()
    Catch
    MsgBox(Err.Number & ": " & Err.Description, , "HK" & CountMe.ToString)
    lblRecursion.Refresh()
    End Try
    End Sub

    Private Sub TestRecursion()
    'Dim x As Double
    'x = Int((max - min + 1) * Rnd() + min)
    'x = 1
    'x = randy.Next(min, max)
    CountMe = CountMe + 1
    'With lblRecursion
    ' .Text = CountMe.ToString
    ' .Refresh()
    'End With
    TestRecursion()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnExit.Click
    End
    End Sub
    </pre>


  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    I am now convinced that the problem in VB 6 is caused by the same/similar circumstances I see in VB .NET.

    First, I'll describe what I found in VB.NET, then I'll describe how I convinced myself the problem is similar in VB 6.
    ------------------------
    In VB .NET.
    --------------
    The problem has nothing to do with the sorting routines. The poor sorting algorithm does make stack overflow occur sooner rather than later, but it has nothing to do with the VB.NET bug preventing my code from trapping stack overflow.

    I created a clean VB .NET project and put 1 label and 2 button controls on the form.

    I then ran the code below.
    Stack overflow occurred in due course and the Try block caught it.

    However, if either, or both of the following is done, the Try block does not catch the stack overflow:

    1. Uncomment the mechanism with Rnd. Problem does not occur using randy.
    2. Uncomment the With ... End With that updates the lblRecursion textbox.

    Doing either, or both, prevents Try from trapping the stack overflow.

    That's one, to be polite, darn bug in VB .NET.

    '----------------------------------------------------
    Private CountMe As Integer
    Private Const min As Integer = 0
    Private Const max As Integer = 999
    'Private randy as New Random

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) _
    Handles btnStart.Click
    CountMe = 0
    Try
    TestRecursion()
    Catch
    MsgBox(Err.Number & ": " & Err.Description, , "HK" &
    CountMe.ToString)
    lblRecursion.Refresh()
    End Try
    End Sub

    Private Sub TestRecursion()
    'Dim x As Double
    'x = Int((max - min + 1) * Rnd() + min)
    'x = 1
    'x = randy.Next(min, max)
    CountMe = CountMe + 1
    'With lblRecursion
    ' .Text = CountMe.ToString
    ' .Refresh()
    'End With
    TestRecursion()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) _
    Handles btnExit.Click
    End
    End Sub
    ------------------------------------------------------
    And now for VB 6.

    Using the code I originally posted for the NoFunction directory, I did the following:

    1. I added the following Function:

    Public Function MyRnd() As Double
    MyRnd = Rnd
    End Function

    2. I changed all reference from Rnd to MyRnd.

    3. This resulted in the same error as before.

    4. I then changed the MyRnd function to the following:

    Public Function MyRnd() As Double
    MyRnd = Now - Date
    End Function

    5. Eureka!!!

    Error no longer occured.

    So, AFTER the stack overflow error occurs, both VB 6 and VB .NET behave badly under the conditions I've described.
    Note that in VB .NET, I was able to contruct a trivial case to isolate the problem.
    Not so in VB 6.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    I believe that I have isolated the cause of the Error 91.

    See http://www.standards.com/temporary/OutOfStackSpace.html

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    It's now clear that VB 6 does not have the ability to clear stack errors as does C, so I'm going to add an option that allows the user to choose, in advance,
    whether to Continue, Exit Sub or End.

    I sure hope this problem was fixed in VS .NET!!

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Losing one's mind tracking down a VB bug

    This thread might be the first known instance of "blogging" in the Lounge.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug


  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Losing one's mind tracking down a VB bug

    Here's the result of my efforts.

    I guess that 29 Feb is an odd day on which to have a birth announcement.

    Up until a few years ago, I used to commit the sin of recommending that
    folkes use WordBasic.SortArray for sorting, well a number of years ago, I
    started some investigations and had an epiphany.

    Along the way, I also noticed that we can do better than use Excel's built-in sort.

    So here's the birth announcement of a program that I finally completed. It
    is useful in determining a better way to sort in VB/VBA.

    I posted the program and documentation at http://www.standards.com/Sorting/SortPerfo...escription.html.

    Code is also posted for most of the sorting routines.

Posting Permissions

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