PDA

Click to See Complete Forum and Search --> : Very odd error, please help


Aragorn
Jul 25th, 2001, 11:29 AM
Hi! it's me again.
I'm in trouble (again): I'm writing a program for simulating a neural network.
I'm working on a graphical interface driven editor for the network. The program displays a window with a picture of the network, and the first thing to do is to be able to drag cells across the window. I use message handling for mouse events and BeginPaint & EndPaint for drawing it. The program compiles fine (nevermind warnings) and runs. Then I select "Network Editor" in the "Window" menu and it opens the editor; fine...
Now I try to drag a cell: the usual way, click on it and move. It goes fine for a while and then it crashes. I get

Thread stopped... : Fault: Access violation at 0xbff51864: write of address 0x40323c

What in earth is happening?????? :confused: :confused: :confused:
After this I click OK and go to the CPU window.
I can't find out what's the problem. I may be doing something wrong but i really don't know what.
Here are the parts of my program which are of some interest:



#define SIZE 30 // Diameter of cell

class cell {
public:
int x,y;

void draw(HDC handle) {
unsigned int i;
Ellipse(handle, (this->x)-SIZE/2, (this->y)-SIZE/2, (this->x)+SIZE/2, (this->y)+SIZE/2);
}
};

class network {
public:
network(){};
network(unsigned int in, unsigned int out, unsigned int middle);

void DrawNetwork(HWND hwnd);
cell* select_mouse_pos(int x, int y);

cell* add_cell(std::vector<cell*> in, std::vector<cell*> out);
cell* add_input(std::vector<cell*> cells);
cell* add_output(std::vector<cell*> cells);

private:
std::vector<cell*> incells; // pointers to input cells in network
std::vector<cell*> outcells; // pointers to output cells in network
std::vector<cell*> maincells; // contains inner cells of network; those that
// do some calculation
};

cell* network::select_mouse_pos(int x, int y) { // Returns pointer to cell pointed by the mouse
double r;
int a, b;
std::vector<cell*>::iterator first=maincells.begin();
std::vector<cell*>::iterator last=maincells.end();

while (first!=last) {
a=(*first)->get_x();
b=(*first)->get_y();
r=sqrt((a-x)*(a-x)+(b-y)*(b-y));
if (r<SIZE/2) return *first;
first++;
}
return NULL;
}

void network::DrawNetwork(HWND hwnd) {
HDC hDispl;
PAINTSTRUCT *lpPaint;
RECT update;
unsigned int i;

if (GetUpdateRect(hwnd, NULL, 0)) {
BeginPaint(hwnd, lpPaint);
hDispl = GetDC(hwnd);
for(i=0; i < incells.size(); i++) {
(*(incells[i])).draw(hDispl);
}
for(i=0; i < maincells.size(); i++) {
(*(maincells[i])).draw(hDispl);
}
for(i=0; i < outcells.size(); i++) {
(*(outcells[i])).draw(hDispl);
}
EndPaint(hwnd, lpPaint);
}
}

void ManageCellDrag(HWND hwnd, cell *p, int x1, int y1) {
if (p==NULL) return;
(*p).set_pos(x1, y1);
InvalidateRect(hwnd, NULL, 1);
PostMessage(NULL, WM_PAINT, 0, 0);
}

LRESULT CALLBACK EditProc(
HWND hewnd,
UINT Message,
WPARAM wParam,
LPARAM lParam) {

static network N(6,6,5); // Here i create the network
static cell *movingCell;

switch(Message) {
case WM_LBUTTONDOWN: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);

SetCapture(hewnd);
movingCell=N.select_mouse_pos(x, y);
if (movingCell!=NULL) ManageCellDrag(hewnd, movingCell, x, y);
return 0;
};
case WM_MOUSEMOVE:{
if (movingCell!=NULL) {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
ManageCellDrag(hewnd, movingCell, x, y);
}
};
break;
case WM_LBUTTONUP: {
ReleaseCapture();
movingCell=NULL;
return 0;
};
case WM_CLOSE:
DestroyWindow(hewnd);
break;
case WM_DESTROY:
// PostQuitMessage(0);
break;
case WM_CREATE:
movingCell=NULL;
break;
case WM_PAINT:
N.DrawNetwork(hewnd);
break;
default:
return DefWindowProc(hewnd, Message, wParam, lParam);
}
return 0;
}



and the loop:


while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}


I'm running on Borland C++ 5.02.
The attachment is my project: At least i would like to know if it crashes on other computers also, since i think this is a very odd error. please try to run it and drag a cell for a while. If after 10 seconds you didn't get an error then i'm in serious trouble!

TRY TO DRAG ONLY CELLS IN THE MIDDLE COLUMN, THE OTHERS DON'T WORK, YET.

I have some guesses, though most probably i'm wrong on it;
1. movingCell eventually pointing to null and trying to paint (*null).draw(). How did it happen?
2. compiler bug (no way, eh?)

i don't know what to do... :(

jim mcnamara
Jul 26th, 2001, 09:59 PM
Try using the assert function. It checks for invalid pointers.

Aragorn
Jul 26th, 2001, 10:58 PM
i have just found out that the program crashes when it calls DrawNetwork a hundred times. :eek:
It may have something to do with the fact that BeginPaint is returning NULL (that's why i have to use GetDC :confused:).
It sounds like a stack not being released or a memory leak or so...
The strange thing is that this also fails: :(


void network::DrawNetwork(HWND hwnd) {
HDC hDispl;
PAINTSTRUCT *lpPaint;
RECT update;
HGDIOBJ Pen;
unsigned int i;
char str[20];
RECT rect;
static unsigned int counter;

if (GetUpdateRect(hwnd, NULL, 1)) {
hDispl = BeginPaint(hwnd, lpPaint);
hDispl = GetDC(hwnd);

itoa(counter++, str, 10); // this is the counter stuff
rect.left=0; // ...
rect.right=0;
rect.top=SIZE*3;
rect.bottom=SIZE*3;
DrawText(hDispl, str, strlen(str), &rect, DT_NOCLIP);// 'till here

for(i=0; i < inCells.size(); i++) {
(*(inCells[i])).draw(hDispl);
}
for(i=0; i < mainCells.size(); i++) {
(*(mainCells[i])).draw(hDispl);
}
for(i=0; i < outCells.size(); i++) {
(*(outCells[i])).draw(hDispl);
}

// here note the now introduced function ReleaseDC

if (ReleaseDC(hwnd, hDispl)!=1) MessageBox(0, "Error: Display Context not released!", "Warning", MB_OK);

EndPaint(hwnd, lpPaint);
}
}


Here I'm releasing everything. Am I not? Well this doesn't work anyway.
I am pretty convinced that it has to do with that NULL pointer i get on BeginPaint, but can't make out why or how to solve it.
Any suggestions? :confused: :confused: :confused: :confused: