I quite don't get it!

I've got a memory access violation on a perfectly normal code string. Error occurs when starting the application. I can click continiuse and everything goes on ok.
And more. If I enter a breakpoint before the bad line it doesn't stop there but the error is gone!

The function I'm referring to doesn't run on startup, it uses no pointers or smth. So as I told, I don't get it.

Can anyone help. Please!

int Field::GetCellX(int x, int y)
{
int i,ii,cx,cy;
double dist;
double mindist;
mindist=10000;
int CellX,CellY;

for (i=0;i<=(this->Picture->Width/(3*this->cellsize));i++)
  for (ii=0;ii<=(this->Picture->Height/(4*this->cellsize));ii++)
  {
    cx=4*this->cellsize/2+i*3*this->cellsize;
    cy=(((i%2)==0)?(4*this->cellsize/2+ii*4*this->cellsize):(4*this->cellsize+ii*4*this->cellsize));
    dist=((cx-x)*(cx-x)+(cy-y)*(cy-y));
    if (dist<mindist)
    {
      CellX=i;
      CellY=ii;
      mindist=dist;             // !!!! THIS IS THE BAD LINE !!!!
    };
  }
  return CellX+this->x;
};

Here is class declaration. Just in case...

class Field
{
 public:
  LivingCell *cell[GameSize*10];
  FieldElement *point[GameSize][GameSize];
  int density[6];  
  TImage *Picture; 
  int cellsize;    
  int x,y;     

  Field();
  void DrawField();
  int GetCellX(int x, int y);
  int GetCellY(int x, int y);
  void Spread(int, int, int);   
  void ClearAgents(void);
};

>>for (i=0;i<=(this->Picture->Width/(3*this->cellsize));i++)
>> for (ii=0;ii<=(this->Picture->Height/(4*this->cellsize));ii++)

Don't you realize the program has to make all those calculations on every loop iteration? That's very very time consuming since the value of Width and cellsize do not change during those loops. It would be much more efficient to just calculate them once above/outside those two loops and use the temp variables inside the loop.

As for your problem: I suspect memory corruption somewhere else. Your program has probably done something to corrupt stack, and the problem just happens to appear on the line you marked. My suggestion is to start commenting stuff out until the problem disappears.

Obviously you can't get access violation at this statement. Possible cause: stack corruption (for example, overwriting of return address) or bad (uninitialized) pointer in your code (outside of GetCellX function call). Try to go through step by step with debugger.

This question has already been answered. Start a new discussion instead.