Valhalla Legends Forums Archive | .NET Platform | [C#] Stack overflow - SOLVED

AuthorMessageTime
Insolence
SOLVED


I'm messing around, porting my AutoIt OCR for Diablo II to C#, this is the PixelSearch function (derived from AutoIt)

I have NO idea why it's causing a stack overflow, if you need more code let me know :)

[code] public int[][] PixelSearch(int startX, int startY, int endX, int endY, int pixel)
{

int width = endX - startX;
int height = endY - startY;
int area = width * height;

int[][] Pixels = new int[area][];

int i = 0;

for ( int x = startX; x < endX; x++ )
{
for ( int y = startY; y < endY; y++ )
{
if ( pixel == GetPixel( x, y ) )
{
Pixels[i] = new int[2] { x, y };
i++;
}
}
}

this.PixelRect = Pixels;
    return Pixels;

}[/code]

Thanks in advance
August 15, 2005, 1:43 AM
Myndfyr
Since you didn't post your solution, I just wanted to point out common reasons for stack overflows.  Generally, StackOverflowExceptions and OutOfMemoryExceptions in .NET are caused by using function code that directly or indirectly calls itself (recursive functions), such as:

[code]
public void CauseAnException() {
  CauseAnException();
}
[/code]

This function will call itself forever until it generates an exception.

For fun, try this out:

[code]
public void CauseAnException() {
  try {
    CauseAnException();
  } catch {
    CauseAnException();
  } finally {
    CauseAnException();
  }
}
[/code]
See if you can get a runtime error ;)
August 15, 2005, 6:40 AM
Insolence
My problem was this:
[code]public int PixelRectLength
{
get { return this.PixelRectLength; }
set { this.PixelRectLength = value; }
}[/code]

I have NO idea why that was failing, and I just copied it from memory.  I simply dropped the getter/setter and made it a member (right?) like so:
[code]public int PixelRectLength;[/code]

EDIT:
I forgot to thank you for being so gratious in your explanation, thank you :)
August 15, 2005, 6:13 PM
Myndfyr
It was failing because it was referencing itself.  You need to have a private member variable, like so:

[code]
private int m_pixelRectLength;
public int PixelRectLength { get { return m_pixelRectLength; } set { m_pixelRectLength = value; } }
[/code]

The way your code would work is like so:
(main code) get_PixelRectLength()
in PixelRectLength, get: get_PixelRectLength()
PixelRectLength
PixelRectLength

Forever.

That's that circular, recursive code that I mentioned in the last post.

And by making it a public variable rather than a private variable with public property, you're making it harder to update your code later.  What if at some point you need to calculate the PixelRectLength value instead of just having a base value?
August 15, 2005, 7:20 PM
Insolence
I'm not sure what you mean by your last statement?

I don't really need the get/set code, so aren't I fine with it the way it is?
August 15, 2005, 10:47 PM
indulgence
You are most likely fine with having a public variable - however its not of the object-oriented mentality of encapsulation
August 25, 2005, 5:36 AM

Search