0

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

  • Comment out/revert recent changes but it still fails
  • Reboot
  • Run it multiple times, it never fails in the same place, but always in a random place

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.

3
  • Strings are reference types, so the length of them wouldn't impact stack usage (the parameter passed on the stack would be a reference to the string), but value types passed as parameters or used as locals could use up stack space. Are you allocating large Structures? Commented Feb 24, 2021 at 14:20
  • Did you introduce a self-referencing property? Commented Feb 24, 2021 at 14:24
  • There are some large classes, but again they would be passed by reference. Some of the functions in there are pretty large with lots of variables, but I can't see how a compiler would use the stack for them. I don't think we have any self referencing properties and no struct types are used in the app. Commented Feb 24, 2021 at 14:50

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is certainly a clue. And implies that even data/processing in the sub (not from a parameter) is contributing to the stack filling up, which I find somewhat puzzling!
@user2728841 Computation won’t (usually) contribute but local variables obviously do (unless they can be elided).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.