'System.StackOverflowException' occurred in System.Drawing.dll

R

Rob Latour

I am getting the following exception which I can not seem to trap:

System.StackOverflowException was unhandled
Message: An unhandled exception of type 'System.StackOverflowException'
occurred in System.Drawing.dll

I've tried :
Try

the offending code



Catch e As StackOverflowException

Catch ex As Exception

End Try

It is not related to recursive code, but rather moving an obect around the
screen.

Any ideas on how I can trap it?

Thanks, Rob
 
C

Carl Daniel [VC++ MVP]

Rob said:
I am getting the following exception which I can not seem to trap:

System.StackOverflowException was unhandled
Message: An unhandled exception of type
'System.StackOverflowException' occurred in System.Drawing.dll

I've tried :
Try

the offending code



Catch e As StackOverflowException

Catch ex As Exception

End Try

It is not related to recursive code, but rather moving an obect
around the screen.

Any ideas on how I can trap it?

It's recursive code, but via a covert path. You're most likely causing your
paint routine to be re-entered, so constant movement of your object on the
screen will consume stack until you stop moving it or the stack runs out,
whichever comes first.

Make sure that the only thing you do in your mouse event handler is
invalidate the window and possibly calculate the new location of the object.
Then let your paint routine refresh the screen but don't do any other work
in Paint.

-cd
 
R

Rob Latour

Thank you - this helps alot - I've spend hours trying to find the cause,
however, my code needs to do a variety of things on the paint event -
including drawing lines and playing with backgrounds. What you have
described is exactly what is happending, the user can fool around with mouse
movements which change the size of the object on the screen and cause its
background to be refreshed for about 15 seconds and then after that the
exception is thrown.

I tried adding this code in the paint handling routine

Static Dim PaintingBeingHandled As Boolean = False
If PaintingBeingHandled Then Exit Sub

PaintingBeingHandled = True

.... My Code ...

PaintingBeingHandled = False

But it didn't help at all. Is there anything else I can do? Change the size
of the stack? Catch the exception?

Thanks, Rob
 
E

EmeraldShield

No, you are going to change how you do your work.
Changing stack size is not to be taken lightly, and you would still run out.

What you need to do it while the mouse is down perform certain calcs, and
then get out of paint. You should never stay in the paint routine. The
paint routine needs to be fast. Your outside routines (Like the mouse trap)
should be doing all the work, and the paint routines should just be
painting.
 
R

Rob Latour

ok thanks

EmeraldShield said:
No, you are going to change how you do your work.
Changing stack size is not to be taken lightly, and you would still run
out.

What you need to do it while the mouse is down perform certain calcs, and
then get out of paint. You should never stay in the paint routine. The
paint routine needs to be fast. Your outside routines (Like the mouse
trap) should be doing all the work, and the paint routines should just be
painting.
 

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

Top