Results 1 to 3 of 3

Thread: Very odd error, please help

Threaded View

  1. #1
    Aragorn
    Guest

    Angry Very odd error, please help

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

    Code:
    #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:

    Code:
    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...
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width