Handler Stack Overflow?!?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tomb

    #1

    Handler Stack Overflow?!?!

    Please help!

    I have a handler in a class that handles when a column in a table is
    modified, as such:

    dtWhatif = dsOp.Tables("wh atif")
    AddHandler dtWhatif.Column Changed, New
    DataColumnChang eEventHandler(A ddressOf Whatif_Changed)

    When testing this, I got an error: An unhandled exception of type
    'System.StackOv erflowException ' occurred in system.dll

    So I put a debug statement in to see why the stack is overflowing. This
    handler was called 4082 times! All I did was change the value in one
    cell of a datagrid!

    Why?! Why?!

    Tom

  • Chris Dunaway

    #2
    Re: Handler Stack Overflow?!?!

    > dtWhatif = dsOp.Tables("wh atif")[color=blue]
    > AddHandler dtWhatif.Column Changed, New
    > DataColumnChang eEventHandler(A ddressOf Whatif_Changed)
    >[/color]

    What happens in Whatif_Changed?

    Comment

    • tomb

      #3
      Re: Handler Stack Overflow?!?!

      Right now, it just calls and empty function. I was just testing the
      handler, and found it doesn't work as expected.

      Tom

      Chris Dunaway wrote:
      [color=blue][color=green]
      >>dtWhatif = dsOp.Tables("wh atif")
      >> AddHandler dtWhatif.Column Changed, New
      >>DataColumnCha ngeEventHandler (AddressOf Whatif_Changed)
      >>
      >>
      >>[/color]
      >
      >What happens in Whatif_Changed?
      >
      >
      >[/color]


      Comment

      • tomb

        #4
        Re: Handler Stack Overflow?!?!

        I just tried commenting out the call to the other function from within
        the event handler. Guess what? No problem. I only get the stack
        overload if the handler calls another function. This is not a good
        thing, because I need to be able to call several functions when the
        column has changed. Any suggestions?

        Tom

        Chris Dunaway wrote:
        [color=blue][color=green]
        >>dtWhatif = dsOp.Tables("wh atif")
        >> AddHandler dtWhatif.Column Changed, New
        >>DataColumnCha ngeEventHandler (AddressOf Whatif_Changed)
        >>
        >>
        >>[/color]
        >
        >What happens in Whatif_Changed?
        >
        >
        >[/color]


        Comment

        • Bob Powell [MVP]

          #5
          Re: Handler Stack Overflow?!?!

          What Chris meant was please post your code. You have obviously generated some race condition here.

          --
          Bob Powell [MVP]
          Visual C#, System.Drawing

          Ramuseco Limited .NET consulting


          Find great Windows Forms articles in Windows Forms Tips and Tricks


          Answer those GDI+ questions with the GDI+ FAQ


          All new articles provide code in C# and VB.NET.
          Subscribe to the RSS feeds provided and never miss a new article.





          "tomb" <tomb@technetce nter.com> wrote in message news:6bfWe.6381 $%Q1.3120@bigne ws3.bellsouth.n et...
          I just tried commenting out the call to the other function from within the event handler. Guess what? No problem. I only get the stack overload if the handler calls another function. This is not a good thing, because I need to be able to call several functions when the column has changed. Any suggestions?

          Tom

          Chris Dunaway wrote:

          dtWhatif = dsOp.Tables("wh atif")
          AddHandler dtWhatif.Column Changed, New
          DataColumnChang eEventHandler(A ddressOf Whatif_Changed)


          What happens in Whatif_Changed?



          Comment

          • tomb

            #6
            Re: Handler Stack Overflow?!?!

            Ok, here's the code:
            Private Sub Whatif_Changed( ByVal sender As Object, ByVal e As
            DataColumnChang eEventArgs)
            Static i As Integer = 0
            'I added i for the debugger
            i += 1

            Debug.WriteLine ("This is cycle number " & i)
            calcWhatif(e.Co lumn.ColumnName , e.Row.Item(0))
            'if calcWhatif is commented out, then this handler is called once,
            otherwise it just keeps being called and the next
            debug line never fires - the call stack overloads first.

            Debug.WriteLine ("Column " & e.Column.Ordina l)

            End Sub

            Right now, calcWhatif doesn't do anything. The code that was going to
            be there can go in the handler, but I still will need to call other
            functions as part of that calculation.

            Thanks for any clarification you can provide.

            Tom

            Bob Powell [MVP] wrote:
            [color=blue]
            > What Chris meant was please post your code. You have obviously
            > generated some race condition here.
            >
            > --
            > Bob Powell [MVP]
            > Visual C#, System.Drawing
            >
            > Ramuseco Limited .NET consulting
            > http://www.ramuseco.com
            >
            > Find great Windows Forms articles in Windows Forms Tips and Tricks
            > http://www.bobpowell.net/tipstricks.htm
            >
            > Answer those GDI+ questions with the GDI+ FAQ
            > http://www.bobpowell.net/faqmain.htm
            >
            > All new articles provide code in C# and VB.NET.
            > Subscribe to the RSS feeds provided and never miss a new article.
            >
            >
            >
            >
            >
            >
            > "tomb" <tomb@technetce nter.com <mailto:tomb@te chnetcenter.com >>
            > wrote in message news:6bfWe.6381 $%Q1.3120@bigne ws3.bellsouth.n et...
            > I just tried commenting out the call to the other function from
            > within the event handler. Guess what? No problem. I only get
            > the stack overload if the handler calls another function. This is
            > not a good thing, because I need to be able to call several
            > functions when the column has changed. Any suggestions?
            >
            > Tom
            >
            > Chris Dunaway wrote:
            >[color=green][color=darkred]
            >>>dtWhatif = dsOp.Tables("wh atif")
            >>> AddHandler dtWhatif.Column Changed, New
            >>>DataColumnCh angeEventHandle r(AddressOf Whatif_Changed)
            >>>
            >>>
            >>>[/color]
            >>
            >>What happens in Whatif_Changed?
            >>
            >>
            >>[/color]
            >[/color]


            Comment

            • Chris Dunaway

              #7
              Re: Handler Stack Overflow?!?!

              tomb wrote:[color=blue]
              > calcWhatif(e.Co lumn.ColumnName , e.Row.Item(0))
              > Debug.WriteLine ("Column " & e.Column.Ordina l)
              >
              > Right now, calcWhatif doesn't do anything. The code that was going to[/color]

              Does calcWhatif have any code in it at all? The behavior you are
              describing sounds like the calcWhatif method is changing the column
              which in turn causes the column changed event to fire which executes
              calcWhatif which changes the column which in turn causes the column
              changed event to fire and on and on until the stack over flows.

              What you can try is declaring a boolean property outside the method,
              for example called bColumnChanging . Then, inside the event, you check
              that boolean:

              If Not bColumnChanging Then
              bColumnChanging = True

              '...rest of code here

              bColumnChanging = False
              End If

              But first you should determine exactly why the stack is overflowing.

              Comment

              • Bob Powell [MVP]

                #8
                Re: Handler Stack Overflow?!?!

                This wouldn't cause a stack overflow. Just the efficient eating up of 100%
                of the processor. It sounds to me as if CalcWhatIf calls CalcWhatIf
                internally and is causing a recursive problem.

                If however the OP refuses to post the actual code instead of being all coy
                about the content of his method then there's not a whole lot we can do ;-)

                --
                Bob Powell [MVP]
                Visual C#, System.Drawing

                Ramuseco Limited .NET consulting


                Find great Windows Forms articles in Windows Forms Tips and Tricks


                Answer those GDI+ questions with the GDI+ FAQ


                All new articles provide code in C# and VB.NET.
                Subscribe to the RSS feeds provided and never miss a new article.





                "Chris Dunaway" <dunawayc@gmail .com> wrote in message
                news:1126806303 .255283.214170@ g44g2000cwa.goo glegroups.com.. .[color=blue]
                > tomb wrote:[color=green]
                >> calcWhatif(e.Co lumn.ColumnName , e.Row.Item(0))
                >> Debug.WriteLine ("Column " & e.Column.Ordina l)
                >>
                >> Right now, calcWhatif doesn't do anything. The code that was going to[/color]
                >
                > Does calcWhatif have any code in it at all? The behavior you are
                > describing sounds like the calcWhatif method is changing the column
                > which in turn causes the column changed event to fire which executes
                > calcWhatif which changes the column which in turn causes the column
                > changed event to fire and on and on until the stack over flows.
                >
                > What you can try is declaring a boolean property outside the method,
                > for example called bColumnChanging . Then, inside the event, you check
                > that boolean:
                >
                > If Not bColumnChanging Then
                > bColumnChanging = True
                >
                > '...rest of code here
                >
                > bColumnChanging = False
                > End If
                >
                > But first you should determine exactly why the stack is overflowing.
                >[/color]


                Comment

                • tomb

                  #9
                  Re: Handler Stack Overflow?!?!

                  I told you already, calcWhatif doesn't do anything yet. I haven't put
                  any code in it other than this:

                  Private Sub calcWhatif(ByRe f sColumn As String, ByRef iRow As String)

                  Dim dr As DataRow
                  Dim drW As DataRow
                  Dim ProdPos As ProductPosition 'a small defined class object

                  Static i As Integer = 0

                  i += 1

                  Debug.WriteLine ("calcWhatif " & i)

                  End Sub

                  I added the debug piece to help me see what was going on.

                  The handler is as follows:

                  Private Sub Whatif_Changed( ByVal sender As Object, ByVal e As
                  DataColumnChang eEventArgs)

                  Static i As Integer = 0

                  i += 1

                  Debug.WriteLine ("This is cycle number " & i)

                  calcWhatif(e.Co lumn.ColumnName , e.Row.Item(0))
                  Debug.WriteLine ("Column " & e.Column.Ordina l)

                  End Sub

                  So, right now the handler is calling an empty function that just returns
                  after outputting the debug info. The problem is that the handler is
                  being called again before returniing from calcWhatif to run the second
                  debug line. I never get to see the column ordinal displayed.

                  But if I take out the call to calcWhatif, everything is fine - I see the
                  ordinal value and the handler runs once. This handler has to be able to
                  call another function, because I have to run calculations based on the
                  changed value.

                  Any insight?

                  Tom


                  Bob Powell [MVP] wrote:
                  [color=blue]
                  >This wouldn't cause a stack overflow. Just the efficient eating up of 100%
                  >of the processor. It sounds to me as if CalcWhatIf calls CalcWhatIf
                  >internally and is causing a recursive problem.
                  >
                  >If however the OP refuses to post the actual code instead of being all coy
                  >about the content of his method then there's not a whole lot we can do ;-)
                  >
                  >
                  >[/color]

                  Comment

                  • tomb

                    #10
                    Re: Handler Stack Overflow?!?! Resolved

                    Would you believe it, when I changed the parameters to being passed
                    byVal, everything was fine. Something to take note of for event
                    handlers - calling another function ByRef explodes!

                    Tom

                    Bob Powell [MVP] wrote:
                    [color=blue]
                    >This wouldn't cause a stack overflow. Just the efficient eating up of 100%
                    >of the processor. It sounds to me as if CalcWhatIf calls CalcWhatIf
                    >internally and is causing a recursive problem.
                    >
                    >If however the OP refuses to post the actual code instead of being all coy
                    >about the content of his method then there's not a whole lot we can do ;-)
                    >
                    >
                    >[/color]

                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Handler Stack Overflow?!?! Resolved

                      Tomb,
                      You also need to be very careful which changing a the value of a column on a
                      row, in the ColumnChanged event handler, as this will cause the event to be
                      raised, which will cause the handler to be called, causing the row to be
                      changed causing the event to be raised, causing the handler to be called,
                      causing the row to be changed, causing the event to be raised, causing ...
                      .... causing Stack Overflow...

                      I normally put a "guard" condition in my handler to exit the handler if a
                      column I am not interested (such as the column I am about to change) has
                      changed... Alternatively I will check to see if only the columns I am
                      interested in have changed, such as the columns that are input into a
                      formula I am calculating in the ColumnChanged event...

                      Hope this helps
                      Jay

                      "tomb" <tomb@technetce nter.com> wrote in message
                      news:JPkWe.1423 3$XZ1.4670@bign ews5.bellsouth. net...
                      | Would you believe it, when I changed the parameters to being passed
                      | byVal, everything was fine. Something to take note of for event
                      | handlers - calling another function ByRef explodes!
                      |
                      | Tom
                      |
                      | Bob Powell [MVP] wrote:
                      |
                      | >This wouldn't cause a stack overflow. Just the efficient eating up of
                      100%
                      | >of the processor. It sounds to me as if CalcWhatIf calls CalcWhatIf
                      | >internally and is causing a recursive problem.
                      | >
                      | >If however the OP refuses to post the actual code instead of being all
                      coy
                      | >about the content of his method then there's not a whole lot we can do
                      ;-)
                      | >
                      | >
                      | >


                      Comment

                      Working...