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;
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++)
    if (dist<mindist)
      mindist=dist;             // !!!! THIS IS THE BAD LINE !!!!
  return CellX+this->x;

Here is class declaration. Just in case...

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

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

Recommended Answers

All 2 Replies

>>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.