this error appears when trying to call a sub (An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll)

i have a sub that will try to get random foods in the database, get the sum of these food's calories and check if they wont exceed the required calories.

most of the time the sub worked, but sometimes this error appears. this is my code. it is kinda long.

Private Sub lunchgenerate()

        Dim grams As Integer = DSgrams.Tables("grams").Rows(0).Item("grams")
        Dim grams1 As Integer = DSricegrams.Tables("ricegrams").Rows(0).Item("grams")
        Dim grams2 As Integer = DSgrams2.Tables("grams").Rows(0).Item("grams")

        Dim calorieval As Decimal = foodcalories * grams
        Dim calorieval1 As Decimal = ricecalories * grams1
        Dim calorieval2 As Decimal = foodcalories2 * grams2

        Dim carbval As Decimal = foodcarb * grams
        Dim carbval1 As Decimal = ricecarb * grams1
        Dim carbval2 As Decimal = foodcarb2 * grams2

        Dim proteinval As Decimal = foodprotein * grams
        Dim proteinval1 As Decimal = riceprotein * grams1
        Dim proteinval2 As Decimal = foodprotein2 * grams

        Dim fatval As Decimal = foodfat * grams
        Dim fatval1 As Decimal = ricefat * grams1
        Dim fatval2 As Decimal = foodfat2 * grams

        Dim caloriepercent As Decimal = usercalories * 0.5
        Dim mincalories As Decimal = caloriepercent - 300

        Dim proteinpercernt As Decimal = userprotein * 0.5
        Dim minprotein As Decimal = proteinpercernt - 20

        Dim counter As Integer = 0
        Dim counter1 As Integer = 0

        Dim foodcalorietotal As Decimal = calorieval + calorieval1 + calorieval2
        Dim foodproteintotal As Decimal = proteinval + proteinval1 + proteinval2
        Dim carbtotal As Decimal = carbval + carbval1 + carbval2
        Dim foodfattotal As Decimal = fatval + fatval1 + fatval2

        If foodcalorietotal < mincalories Or foodcalorietotal > caloriepercent Then
            counter = 0
        Else
            counter = 1
        End If

        If foodproteintotal < minprotein Or foodproteintotal > proteinpercernt Then
            counter1 = 0
        Else
            counter1 = 1
        End If

        If counter = 1 And counter1 = 1 Then
          'output to the form

        Else

                **lunchgenerate()**

            End If

        End If
End Sub

i think the error is when the lunchgenerate() sub is called again.

but just like what i said, most of the times it worked, but sometimes it freezes and then this error appears and then highlights the first line of my code which is

        Dim DAlunchcategory As New SqlDataAdapter(lunchquery, CN)
        Dim DSlunchcategory As New DataSet
        DAlunchcategory.Fill(DSlunchcategory, "category_id")

Upvotes: 0

Views: 104

Answers (1)

Igor
Igor

Reputation: 15893

Stack Overflow error happens when there is no space on the stack (part of the process memory used for local variables, parameter values, function results and a few other little things).

Often the code that produces this error does it by executing infinite recursion. If the code that you showed is complete lunchgenerate function, in the case when either counter or counter1 is not 1, lunchgenerate will be called again with exactly the same outcome, and again, and again, until it completely depletes space on the stack and exception is thrown. You need to have some escape logic to avoid it.

Upvotes: 1

Related Questions