none
Visual Studio 2010 - Unhandled exception at 0x01b5fee9: 0xC0000005: Access violation reading location 0x01b6e002. RRS feed

  • Pergunta

  • Hello, MSDN.

    My problem is a bit odd. I'm compile an OpenGL program, but when I try to end my application, the following error occurs:

     

    First-chance exception at 0x01b5fee9 in OpenGL.exe: 0xC0000005: Access violation reading location 0x01b6e002.
    Unhandled exception at 0x01b5fee9 in OpenGL.exe: 0xC0000005: Access violation reading location 0x01b6e002.

     

    The worst thing is that I can't debug the code, because there's no source at all! Apparently, it happens after my function returns.
    In any case, here is my code:

    main.cpp

    #include "main.h"
    
    UINT timer = 0;
    UINT prevTimer = 0;
    UINT ticks = 0;
    
    char str[128];
    GLuint texture;
    
    LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    	switch (msg) {
    	case WM_DESTROY:
    	case WM_CLOSE:
    		PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd) {
    	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_OWNDC, MsgProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, ClassName, NULL };
    	RegisterClassEx(&wc);
    
    	windowWidth = 640, windowHeight = 480;
    	hWnd = CreateWindow(ClassName, "GL BEGIN", WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME, 32, 32, windowWidth, windowHeight, GetDesktopWindow(), NULL, hInstance, NULL);
    
    	OutputDebugString(__FILE__);
    	GL::Initialize();
    	GL::LoadResources();
    
    	ShowWindow(hWnd, nShowCmd);
    	UpdateWindow(hWnd);
    
    	MSG msg;
    	ZeroMemory(&msg, sizeof(MSG));
    	bool running = true;
    	while (running) {
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    			if (msg.message == WM_QUIT) running = false;
    			else {
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    		} else GL::PaintScreen();
    	}
    	GL::Terminate();
    	DestroyWindow(hWnd);
    	UnregisterClass(ClassName, hInstance);
    	return msg.wParam;
    }
    
    inline void GL::Initialize() {
    	// Get window's DC
    	hDC = GetDC(hWnd);
    
    	// Select a pixel format descriptor
    	PIXELFORMATDESCRIPTOR pfd;
    	ZeroMemory(&pfd, sizeof(pfd));
    	pfd.nSize = sizeof(pfd);
    	pfd.nVersion = 1;
    	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    	pfd.iPixelType = PFD_TYPE_RGBA;
    	pfd.cColorBits = 24;
    	pfd.cDepthBits = 16;
    	pfd.iLayerType = PFD_MAIN_PLANE;
    
    	int iFormat = ChoosePixelFormat(hDC, &pfd);
    	SetPixelFormat(hDC, iFormat, &pfd);
    
    	// Get an RC
    	hRC = wglCreateContext(hDC);
    	wglMakeCurrent(hDC, hRC);
    }
    
    inline void GL::LoadResources() {
    	glMatrixMode(GL_PROJECTION);
    	glOrtho(0, 640, 480, 0, 0, 256);
    
    	glGenTextures(1, &texture);
    	glBindTexture(GL_TEXTURE_2D, texture);
    	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    
    	FILE *file;
    	BITMAPFILEHEADER bmfh;
    	BITMAPINFOHEADER bmih;
    
    	fopen_s(&file, "Battler.bmp", "rb");
    	fread(&bmfh, sizeof(BITMAPFILEHEADER), 1, file);
    	fread(&bmih, sizeof(BITMAPINFOHEADER), 1, file);
    
    	RGBQUAD colors[256];
    	fread(colors, sizeof(RGBQUAD), 256, file);
    	DWORD size = bmfh.bfSize - bmfh.bfOffBits;
    
    	BYTE *tempPixelData = new BYTE[size];
    	fread(tempPixelData, sizeof(BYTE), size, file);
    
    	bool isInversed = bmih.biHeight < 0;
    	if (isInversed) bmih.biHeight = -bmih.biHeight;
    	BYTE *buffer = new BYTE[bmih.biWidth * bmih.biHeight * 3];
    	UINT ySize;
    	for (INT y = 0; y < bmih.biHeight; y++) {
    		for (INT x = 0; x < bmih.biWidth; x++) {
    			ySize = isInversed ? bmih.biHeight - y : y;
    			memcpy(buffer+(bmih.biWidth*ySize + x)*3, colors+tempPixelData[bmih.biWidth*y+x], 3);;
    		}
    	}
    
    	glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
    	
    	fclose(file);
    	delete[] tempPixelData;
    	delete[] buffer;
    }
    
    inline void GL::Terminate() {
    	wglMakeCurrent(NULL, NULL);
    	wglDeleteContext(hRC);
    	ReleaseDC(hWnd, hDC);
    }
    
    void GL::PaintScreen() {
    	glClearColor(0, 0, 0, 0);
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	glBindTexture(GL_TEXTURE_2D, texture);
    	glColor3f(1, 1, 1);
    	glBegin(GL_QUADS);
    	{
    		glTexCoord2f(0, 0); glVertex2i(0, 0);
    		glTexCoord2f(1, 0); glVertex2i(640, 0);
    		glTexCoord2f(1, 1); glVertex2i(640, 480);
    		glTexCoord2f(0, 1); glVertex2i(0, 480);
    	}
    	glEnd();
    
    	timer++;
    
    	SwapBuffers(hDC);
    }
    

     

    main.h:

    #include <Windows.h>		// Windows core functions
    #include <gl\GL.h>			// GL core functions
    #include <gl\GLU.h>
    
    #include <cmath>
    #include <cstdio>
    
    #define PI 3.1415926535897932384626433832795
    #define ClassName "GLWin"
    
    HWND hWnd;
    int windowWidth, windowHeight;
    
    namespace GL {
    	inline void Initialize();
    	inline void LoadResources();
    	inline void Terminate();
    	void PaintScreen();
    
    	HDC hDC;
    	HGLRC hRC;
    }

    What would it be? There are only these two files.

     


    terça-feira, 19 de abril de 2011 22:16

Todas as Respostas

  • It seems strange for me that you are calling DestroyWindow(..) after the message loop has received WM_QUIT message. Is there any reason for that?

     

    cox


    cox
    quarta-feira, 20 de abril de 2011 18:15
  • Well, the WM_QUIT is received by the WM_CLOSE, not by the WM_DESTROY.

    By the way, I fixed my problem. By some reason, the memcpy gives this error. I replaced my bitmap by a 24-bit bitmap and changed the load routine.

    • Sugerido como Resposta Levi Domingos quarta-feira, 15 de junho de 2011 23:02
    quarta-feira, 20 de abril de 2011 21:46