Handler Stack Overflow?!?!

  • Thread starter Thread starter tomb
  • Start date Start date
T

tomb

Please help!

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

dtWhatif = dsOp.Tables("whatif")
AddHandler dtWhatif.ColumnChanged, New
DataColumnChangeEventHandler(AddressOf Whatif_Changed)

When testing this, I got an error: An unhandled exception of type
'System.StackOverflowException' 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
 
dtWhatif = dsOp.Tables("whatif")
AddHandler dtWhatif.ColumnChanged, New
DataColumnChangeEventHandler(AddressOf Whatif_Changed)

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

Tom
 
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
 
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.





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("whatif")
AddHandler dtWhatif.ColumnChanged, New
DataColumnChangeEventHandler(AddressOf Whatif_Changed)


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

Debug.WriteLine("This is cycle number " & i)
calcWhatif(e.Column.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.Ordinal)

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
 
tomb said:
calcWhatif(e.Column.ColumnName, e.Row.Item(0))
Debug.WriteLine("Column " & e.Column.Ordinal)

Right now, calcWhatif doesn't do anything. The code that was going to

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.
 
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
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.
 
I told you already, calcWhatif doesn't do anything yet. I haven't put
any code in it other than this:

Private Sub calcWhatif(ByRef 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
DataColumnChangeEventArgs)

Static i As Integer = 0

i += 1

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

calcWhatif(e.Column.ColumnName, e.Row.Item(0))
Debug.WriteLine("Column " & e.Column.Ordinal)

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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top