catching Runtime Stack Overflow Exception

A

assaf

hi robert.

it is not recurssion!
just the same function name.
this system works very well 99.99% percent of the time.
it only crashes in cases of abnormally high user input.
in which case, i would like the exception to be caught and ignored!!!

assaf



Robert Jordan said:
hi,
private RectangleF[] GetRegionScans(Region re)
{
Matrix m = new Matrix();
m.Reset();
RectangleF[] rects = re.GetRegionScans(m); // this line throws the
StackOverflowException!!!
return rects;
}

Are you kidding? :) However, try this:

private RectangleF[] GetRegionScans(Region re)
{
Matrix m = new Matrix();
m.Reset();
// endless recursion condition
if (re != this) {
RectangleF[] rects = re.GetRegionScans(m);
return rects;
}
else {
return whatever but don't recurse!
}
}

bye
Rob
 
J

Jon Skeet [C# MVP]

assaf said:
here is the code.
it is not so pretty.
but really its nothing more then:
put many rects into region.
get rects array from region -> crash
u can try it yourselves.

How on earth can we try it for ourselves when you haven't given the
code for the method which is crashing?

Please post a short but complete program which demonstrates the
problem.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
A

assaf

hi Jon

run this program.
it crashes at the line:
RectangleF [] rcf = r.GetRegionScans(m); // StackOverflowException
i just need to catch it,
nothing more.

assaf



using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Stack02
{
class Class1
{
static void Main()
{
try
{
Region r = new Region();
r.MakeEmpty();

for(int y = 0; y < 768; y += 2)
{
for(int x = 0; x < 1024; x += 2)
{
Rectangle rc = new Rectangle(x, y, 5, 5);
r.Union(rc);
}
}
Matrix m = new Matrix();
m.Reset();
RectangleF [] rcf = r.GetRegionScans(m); // StackOverflowException
}
catch(StackOverflowException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
 
J

Jon Skeet [C# MVP]

assaf said:
run this program.
it crashes at the line:
RectangleF [] rcf = r.GetRegionScans(m); // StackOverflowException

Right, thanks for the test case.
i just need to catch it, nothing more.

I think you really will find this hard to do robustly, to be honest.

Your best bet may end up being to rewrite the method yourself, if
possible, and make sure it doesn't recurse internally. Alternatively,
make sure you never have regions with that many rectangles - split them
and then reunite them later.

Sorry not to have better news :(
 
A

assaf

ok.
tnx

assaf


Jon Skeet said:
assaf said:
run this program.
it crashes at the line:
RectangleF [] rcf = r.GetRegionScans(m); // StackOverflowException

Right, thanks for the test case.
i just need to catch it, nothing more.

I think you really will find this hard to do robustly, to be honest.

Your best bet may end up being to rewrite the method yourself, if
possible, and make sure it doesn't recurse internally. Alternatively,
make sure you never have regions with that many rectangles - split them
and then reunite them later.

Sorry not to have better news :(
 

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