Home All Groups Group Topic Archive Search About

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

Author
4 Oct 2006 3:28 AM
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

Author
4 Oct 2006 3:59 AM
Carl Daniel [VC++ MVP]
Rob Latour wrote:
Show quote
> 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
Author
4 Oct 2006 10:56 AM
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


Show quote
> 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.
Author
4 Oct 2006 7:59 PM
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.



Show quote
"Rob Latour" <roblat***@rogers.com> wrote in message
news:eJ7WyO65GHA.4644@TK2MSFTNGP04.phx.gbl...
> 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
>
>
>> 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.
>
>
Author
4 Oct 2006 9:00 PM
Rob Latour
ok thanks

Show quote
"EmeraldShield" <emeraldshield@noemail.noemail> wrote in message
news:%23XMiK%23%235GHA.140@TK2MSFTNGP03.phx.gbl...
> 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.
>
>
>
> "Rob Latour" <roblat***@rogers.com> wrote in message
> news:eJ7WyO65GHA.4644@TK2MSFTNGP04.phx.gbl...
>> 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
>>
>>
>>> 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.
>>
>>
>
>

AddThis Social Bookmark Button