user2728841
user2728841

Reputation: 1427

c#/vb Stack Overflow Exception on relatively small call stack

I'm getting a stack overflow exception in my VB.NET DLL.

The call stack is 115 calls deep, and its not a buggy recursive program, but that depth of call stack is genuinely needed.

I'm wondering why it would bomb out when I imagine that recursive programs go much deeper than this. Does anyone know what contributes to the stack space being used up ? e.g. does a long string as a parameter in one of the calls contribute ?? or otherwise what else would be causing it ?

I've tried the following

The code is single threaded at the moment and is compiled as a 64 bit process.

I have not upgraded VS for about a year so not sure what changed to cause this

I'm aware of how to fix this by launching parts of the process on different threads and then waiting for them, but that solution worries me because it's more of a workaround/hack than finding what caused it in the first place.

So looking to understand possible factors that may be causing the error and thoughts on what might contribute to the stack space filling up so quickly.

I'm sure the program would continue to error if migrated in C#, so I've tagged with C# also since the error has the same potential to happen in that language.

Upvotes: 0

Views: 107

Answers (1)

dbasnett
dbasnett

Reputation: 11773

The depth will be affected by what is going on within the methods. I created this example that gets different counts based on the compiler variable foo. When foo is true I get 2799 for a count, false the count is 5364. I had to play with the positioning of the #if to be able to see the count.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        test()
    End Sub

    Dim count As Integer = 0

#Const foo = True
    Private Sub test()
        Try
            count = count
            count += 1

            Dim xe As XElement = <breakfast_menu></breakfast_menu>
            xe.Add(<food>
                       <name>Belgian Waffles</name>
                       <price>$5.95</price>
                       <calories>650</calories>
                   </food>)
            xe.Add(<food>
                       <name>Strawberry Belgian Waffles</name>
                       <price>$7.95</price>
                       <calories>900</calories>
                   </food>)
#If foo Then
            xe.Add(<food>
                       <name>Berry-Berry Belgian Waffles</name>
                       <price>$8.95</price>
                       <calories>900</calories>
                   </food>)
            xe.Add(<food>
                       <name>French Toast</name>
                       <price>$4.50</price>
                       <calories>600</calories>
                   </food>)
            xe.Add(<food>
                       <name>Homestyle Breakfast</name>
                       <price>$6.95</price>
                       <calories>950</calories>
                   </food>)

            Dim ie As IEnumerable(Of XElement)
            ie = From el In xe.<food>
                 Where Integer.Parse(el.<calories>.Value) < 900
                 Select el
#End If
            test()

        Catch ex As Exception
            Stop
        End Try
    End Sub

Upvotes: 1

Related Questions