• Welcome to Valhalla Legends Archive.
 

[C#] Stack overflow - SOLVED

Started by Insolence, August 14, 2005, 08:43 PM

Previous topic - Next topic

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

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;

}


Thanks in advance

MyndFyre

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:


public void CauseAnException() {
  CauseAnException();
}


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

For fun, try this out:


public void CauseAnException() {
  try {
    CauseAnException();
  } catch {
    CauseAnException();
  } finally {
    CauseAnException();
  }
}

See if you can get a runtime error ;)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Insolence

#2
My problem was this:
public int PixelRectLength
{
get { return this.PixelRectLength; }
set { this.PixelRectLength = value; }
}


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:
public int PixelRectLength;

EDIT:
I forgot to thank you for being so gratious in your explanation, thank you :)

MyndFyre

It was failing because it was referencing itself.  You need to have a private member variable, like so:


private int m_pixelRectLength;
public int PixelRectLength { get { return m_pixelRectLength; } set { m_pixelRectLength = value; } }


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?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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?

indulgence

You are most likely fine with having a public variable - however its not of the object-oriented mentality of encapsulation
<3