1 Replies - 398 Views - Last Post: 18 July 2011 - 03:59 PM Rate Topic: -----

#1 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Run-Time Check Failure #2

Posted 18 July 2011 - 02:21 PM

The program compiles, and runs, but then crashes, this is what I am getting.

Run-Time Check Failure #2 - Stack around the variable 'str' was corrupted.

I will be honest, I do not understand what is wrong, I think maybe I put a piece of code in the wrong position?
Here is the code, any help you can give will be deeply appreciated. Thank you for your time.

 /*
main.cpp

simple OpenGL demo
*/

#include <windows.h>        // must include this for windows programs
#include <stdio.h>

#include "resource.h"

#include <gl/gl.h>
#include <gl/glu.h>

#include "CGfxOpenGL.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


// Global variables to store dialog and picture box window handles
HWND HOutput = NULL;  // handle to the output control
HWND HSample = NULL;
HWND HDialog = NULL;  // handle to the main dialog window
bool Exiting = false;
long WindowWidth;
long WindowHeight;
long WindowBits = 32;
bool Fullscreen = false;
float ObjPosX = 0.0;
float ObjVelX = 0.0;
CGfxOpenGL *g_glRender = NULL;

bool Wireframe = true;

// current camera settings
int PosX = 0;
int PosY = 10;
int PosZ = 50;
int LookX = 0;
int LookY = 0;
int LookZ = 0;
int TransOffset = 50;
int Roll = 0;
int FOV = 45;  // range 20..100??

Vertex3F SphereCenter;
int MouseX = 0;
int MouseY = 0;

void SetupPixelFormat(HDC hDC)
{
    int pixelFormat;

    PIXELFORMATDESCRIPTOR pfd =
    {   
        sizeof(PIXELFORMATDESCRIPTOR),  // size
            1,                          // version
            PFD_SUPPORT_OPENGL |        // OpenGL window
            PFD_DRAW_TO_WINDOW |        // render to window
            PFD_DOUBLEBUFFER,           // support double-buffering
            PFD_TYPE_RGBA,              // color type
            32,                         // prefered color depth
            0, 0, 0, 0, 0, 0,           // color bits (ignored)
            0,                          // no alpha buffer
            0,                          // alpha bits (ignored)
            0,                          // no accumulation buffer
            0, 0, 0, 0,                 // accum bits (ignored)
            16,                         // depth buffer
            0,                          // no stencil buffer
            0,                          // no auxiliary buffers
            PFD_MAIN_PLANE,             // main layer
            0,                          // reserved
            0, 0, 0,                    // no layer, visible, damage masks
    };

    pixelFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, pixelFormat, &pfd);
}


/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
                          UINT message, 
                          WPARAM wParam, 
                          LPARAM lParam)
{
    static HDC hDC;
    static HGLRC hRC;
    HWND hCtrl;
    int ctrlID;
    int pos, min, max;
    RECT rect;
    HBRUSH brush;
    int x, y;

	    // switch based on message type
    switch(message)
    {

    case WM_LBUTTONDOWN:
            // get the mouse position
            x = LOWORD(lParam);
            y = HIWORD(lParam);


        break;

    case WM_INITDIALOG:
        // dialog is initializing – 
        // store the picture box handle in a global variable for later 
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        
		hDC = GetDC(HOutput);

        // initialize the rgb and hsv scroll bars
        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_POSX);
        SetScrollRange(hCtrl, SB_CTL, 0, 100, true);
        SetScrollPos(hCtrl, SB_CTL, PosX + TransOffset, true);
		SetDlgItemInt(hwnd, IDC_STATIC_POSX, PosX, true);

        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_POSY);
        SetScrollRange(hCtrl, SB_CTL, 0, 100, true);
        SetScrollPos(hCtrl, SB_CTL, PosY + TransOffset, true);
		SetDlgItemInt(hwnd, IDC_STATIC_POSY, PosY, true);

        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_POSZ);
        SetScrollRange(hCtrl, SB_CTL, 1, 100, true);
        SetScrollPos(hCtrl, SB_CTL, PosZ, true);
		SetDlgItemInt(hwnd, IDC_STATIC_POSZ, PosZ, true);

        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_LOOKX);
        SetScrollRange(hCtrl, SB_CTL, 0, 100, true);
        SetScrollPos(hCtrl, SB_CTL, LookX + TransOffset, true);
		SetDlgItemInt(hwnd, IDC_STATIC_LOOKX, LookX, true);

        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_LOOKY);
        SetScrollRange(hCtrl, SB_CTL, 0, 100, true);
        SetScrollPos(hCtrl, SB_CTL, LookY + TransOffset, true);
		SetDlgItemInt(hwnd, IDC_STATIC_LOOKY, LookY, true);

        hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_LOOKZ);
        SetScrollRange(hCtrl, SB_CTL, 0, 100, true);
        SetScrollPos(hCtrl, SB_CTL, LookZ + TransOffset, true);
		SetDlgItemInt(hwnd, IDC_STATIC_LOOKZ, LookZ, true);

		hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_ROLL);
        SetScrollRange(hCtrl, SB_CTL, 0, 360, true);
        SetScrollPos(hCtrl, SB_CTL, Roll, true);
		SetDlgItemInt(hwnd, IDC_STATIC_ROLL, Roll, true);

		hCtrl = GetDlgItem(hwnd, IDC_SCROLLBAR_FOV);
        SetScrollRange(hCtrl, SB_CTL, 10, 120, true);
        SetScrollPos(hCtrl, SB_CTL, FOV, true);
		SetDlgItemInt(hwnd, IDC_STATIC_FOV, FOV, true);


        // set up the opengl render window
        SetupPixelFormat(hDC);
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
        break;

	case WM_TIMER:

      ObjPosX = ObjPosX = ObjPosX + (20.0/1000.0) * ObjVelX; //add code to position based on formula
                     //xnew = xold + dt*velocity
                     //Be sure to use the same time as set up in SetTimer:
                     //dt=20.0/1000.0

     char str[32];
                sprintf(str, "%s <%.2f %.2f %.2f>", "Joe Eggemeyer", ObjPosX, 0.0F, 0.0F); //don't make your name too long
      SetWindowText(HDialog, str);
     break;  

    case WM_DESTROY:            // window destroy
    case WM_QUIT:
    case WM_CLOSE:                  // windows is closing

        // deselect rendering context and delete it
        wglMakeCurrent(hDC, NULL);
        wglDeleteContext(hRC);

        // send WM_QUIT to message queue
        PostQuitMessage(0);
        break;


    case WM_HSCROLL:
        // one of the scroll bars has moved

        // update the scroll bars
        hCtrl = (HWND)lParam;
        ctrlID = GetWindowLong(hCtrl, GWL_ID);
        pos = GetScrollPos(hCtrl, SB_CTL);
        GetScrollRange(hCtrl, SB_CTL, &min, &max);
        switch(LOWORD(wParam)) {
            case SB_LINEUP:
            case SB_PAGEUP:
                pos -= 1;
                pos = MAX(pos, min);
            break;
            case SB_LINEDOWN:
            case SB_PAGEDOWN:
                pos += 1;
                pos = MIN(pos, max);
            break;
            case SB_THUMBPOSITION:
            case SB_THUMBTRACK:
                pos = HIWORD(wParam);
            break;
        }

        SetScrollPos(hCtrl, SB_CTL, pos, true);
        switch(ctrlID) {
            case IDC_SCROLLBAR_DX:
			    PosX = GetScrollPos(hCtrl, SB_CTL);
				PosX -= TransOffset;
				SetDlgItemInt(hwnd, IDC_STATIC_POSX, PosX, true);
                break;
            case IDC_SCROLLBAR_DY:
			    PosY = GetScrollPos(hCtrl, SB_CTL);
				PosY -= TransOffset;
				SetDlgItemInt(hwnd, IDC_STATIC_POSY, PosY, true);
                break;
            case IDC_SCROLLBAR_DZ:
			    PosZ = GetScrollPos(hCtrl, SB_CTL);
				SetDlgItemInt(hwnd, IDC_STATIC_POSZ, PosZ, true);
                break;
            case IDC_SCROLLBAR_LOOKX:
			    LookX = GetScrollPos(hCtrl, SB_CTL);
				LookX -= TransOffset;
				SetDlgItemInt(hwnd, IDC_STATIC_LOOKX, LookX, true);
                break;
            case IDC_SCROLLBAR_LOOKY:
			    LookY = GetScrollPos(hCtrl, SB_CTL);
				LookY -= TransOffset;
				SetDlgItemInt(hwnd, IDC_STATIC_LOOKY, LookY, true);
                break;
            case IDC_SCROLLBAR_LOOKZ:
			    LookZ = GetScrollPos(hCtrl, SB_CTL);
				LookZ -= TransOffset;
				SetDlgItemInt(hwnd, IDC_STATIC_LOOKZ, LookZ, true);
                break;
            case IDC_SCROLLBAR_ROLL:
			    Roll = GetScrollPos(hCtrl, SB_CTL);
				SetDlgItemInt(hwnd, IDC_STATIC_ROLL, Roll, true);
                break;
            case IDC_SCROLLBAR_FOV:
			    FOV = GetScrollPos(hCtrl, SB_CTL);
				SetDlgItemInt(hwnd, IDC_STATIC_SCALE, FOV, true);
                break;
        }

        // render opengl stuff
		g_glRender->Prepare(0.0f);
        g_glRender->Render();
        SwapBuffers(hDC);
        break;

        /* no buttons used in this application
        case WM_COMMAND:
            // the WM_COMMAND message is for button presses among other things

            // decide which command or button was activated
            switch(LOWORD(wParam))
            {
            case IDC_BUTTON_DRAW:
                // our draw button was pressed
			    Height += 1;
			    if (Height > MaxConeHt) {
				    Height = MaxConeHt;
			    }
                break;
            case IDC_BUTTON_CLEAR:
                // our draw button was pressed
			    Height -= 1;
			    if (Height < MinConeHt) {
				    Height = MinConeHt;
			    }
                break;

            case IDC_BUTTON_QUIT:
            case IDCANCEL:
                // end the program if title bar X is pressed or other cancel message received


			    // deselect rendering context and delete it
			    wglMakeCurrent(hDC, NULL);
			    wglDeleteContext(hRC);

                // posting a quit message will end our windows application
                PostQuitMessage(0);
                break;
            }

        break;
        /* */
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
	HDC hDC;
	RECT rect;

    g_glRender = new CGfxOpenGL;

    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);


	SetFocus(NULL);
    hDC = GetDC(HOutput);

    g_glRender->Init();

	GetClientRect(HOutput, &rect);
    WindowWidth = rect.right - rect.left;
    WindowHeight = rect.bottom - rect.top;

    g_glRender->SetupProjection(WindowWidth, WindowHeight);

	SetTimer(HDialog, 1, 20, NULL);      //20 milliseconds

    // standard windows message loop
    MSG  msg;
    while (!Exiting)
    {
        // render opengl stuff
		g_glRender->Prepare(0.0f);
        g_glRender->Render();
        SwapBuffers(hDC);
		
		if (KEY_DOWN(VK_RIGHT)) {

 

		ObjVelX = ObjVelX + 0.1f;;  //Add you own code to update the object’s horizontal velocity 
                  //so that when the right key is pressed the velocity increases
                  //i.e., it goes faster and faster        
		}

		if (KEY_DOWN(VK_LEFT)) {

 

		ObjVelX = ObjVelX - 0.1f;;  //Add you own code to update the object’s horizontal velocity 
                  //so that when the left key is pressed the velocity increases
                  //i.e., it goes faster and faster        
		}

        // handle window message stuff
        if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (!GetMessage (&msg, NULL, 0, 0))
            {
                Exiting = true;
                break;
            }

			// avoid processing messages for the dialog
			if (!IsDialogMessage (HDialog, & msg))
			{
				TranslateMessage ( & msg );
				DispatchMessage ( & msg );
			}
        }
    }

    return (int)(msg.wParam);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Run-Time Check Failure #2

#2 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Run-Time Check Failure #2

Posted 18 July 2011 - 03:59 PM

Actually, I figured this out, I just needed to allocate more space to str. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1