Access violation reading location 0xfeeeff0a

This is a discussion on Access violation reading location 0xfeeeff0a within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new in this forum and the c++ world, I come from the java world and I have some ...

  1. #1
    rXp
    rXp is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Question Access violation reading location 0xfeeeff0a

    Hello,

    I'm new in this forum and the c++ world, I come from the java world and I have some difficulties getting used to the way things work here but I'm learning.
    I bought the Beginning game programming third edition book and learning directx with it, for now it is quite simple but I still have the "newbies errors"...
    I hope you can help me with this one :
    I'm creating a little class to help me manage the inputs (keyboard, mouse, controllers) but when I want to use the method that get the mouse position or keyboard keys I got this error :
    Code:
    First-chance exception at 0x002f13ec in xinputd3d.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
    Unhandled exception at 0x002f13ec in xinputd3d.exe: 0xC0000005: Access violation reading location 0xfeeeff0a.
    My code is still a bit messy but it is just test code, so :
    winmain.cpp :
    Code:
    /*
        Beginning Game Programming, Third Edition
        Chapter 4
        Create_Surface program
    */
    
    #include <windows.h>
    #include <sstream>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <dinput.h>
    #include <Xinput.h>
    #include "InputHelper.h"
    #include <time.h>
    using namespace std;
    
    #pragma comment(lib,"d3d9.lib")
    #pragma comment(lib,"d3dx9.lib")
    #pragma comment(lib,"dinput8.lib")
    #pragma comment(lib,"dxguid.lib")
    
    //application title
    const string APPTITLE = "Test input program";
    
    //screen resolution
    #define SCREENW 1024
    #define SCREENH 768
    
    //Direct3D objects
    LPDIRECT3D9 d3d = NULL; 
    LPDIRECT3DDEVICE9 d3ddev = NULL; 
    LPDIRECT3DSURFACE9 backbuffer = NULL;
    LPDIRECT3DSURFACE9 surface = NULL;
    InputHelper input = NULL;
    
    bool gameover = false;
    
    /**
     ** Game initialization function
     **/
    bool Game_Init(HWND hwnd)
    {
        //initialize Direct3D
        d3d = Direct3DCreate9(D3D_SDK_VERSION);
        if (d3d == NULL)
        {
            MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
            return false;
        }
    
        //set Direct3D presentation parameters
        D3DPRESENT_PARAMETERS d3dpp; 
        ZeroMemory(&d3dpp, sizeof(d3dpp));
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
        d3dpp.BackBufferCount = 1;
        d3dpp.BackBufferWidth = SCREENW;
        d3dpp.BackBufferHeight = SCREENH;
        d3dpp.hDeviceWindow = hwnd;
    
        //create Direct3D device
        d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
    
        if (!d3ddev)
        {
            MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
            return false;
        }
    
        //set random number seed
        srand(time(NULL));
    
        //clear the backbuffer to black
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
        
        //create pointer to the back buffer
        d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
    
        //create surface
        HRESULT result = d3ddev->CreateOffscreenPlainSurface(
            800,                //width of the surface
            600,                //height of the surface
            D3DFMT_X8R8G8B8,    //surface format
            D3DPOOL_DEFAULT,    //memory pool to use
            &surface,           //pointer to the surface
            NULL);              //reserved (always NULL)
    
        if (!SUCCEEDED(result)) return false;
    
    	input = InputHelper(hwnd);
    
        return true;
    }
    
    /**
     ** Game update function
     **/
    void Game_Run(HWND hwnd)
    {
        //make sure the Direct3D device is valid
        if (!d3ddev) return;
    
    	//bb
    	d3ddev->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer);
        //start rendering
        if (d3ddev->BeginScene())
        {    
            //stop rendering
            d3ddev->EndScene();
    
            //display the back buffer on the screen
            d3ddev->Present(NULL, NULL, NULL, NULL);
        }
    
    	//acquire and get the keys
    	//vector<char> keysPressed = input.getKeysPressed(hwnd);
    	/*for each(char c in keysPressed)
    	{
    		if(c == DIK_ESCAPE)
    		{
    			PostMessage(hwnd, WM_DESTROY,NULL,NULL);
    		}
    	}*/
    	//long *d = input.getMouseLocation(2);
    	long d[2];
    	input.getMouseLocation(d); // <-------------------------------------------------------- the error is here
    	stringstream s;
    	s << d[0];
    	MessageBox(hwnd, s.str().c_str(),"test2",MB_OK);
    }
    
    /**
     ** Game shutdown function
     **/
    void Game_End(HWND hwnd)
    {
    	input.~InputHelper();
        if (surface) surface->Release();
        if (d3ddev) d3ddev->Release();
        if (d3d) d3d->Release();
    }
    
    
    /**
     ** Windows event callback function
     **/
    LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
            case WM_DESTROY:
                gameover = true;
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc( hWnd, msg, wParam, lParam );
    }
    
    
    /**
     ** Windows entry point function
     **/
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        //create the window class structure
        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX); 
        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = (WNDPROC)WinProc;
        wc.cbClsExtra     = 0;
        wc.cbWndExtra     = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = NULL;
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = APPTITLE.c_str();
        wc.hIconSm       = NULL;
        RegisterClassEx(&wc);
    
        //create a new window
        HWND window = CreateWindow(APPTITLE.c_str(), APPTITLE.c_str(), 
           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
           SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
    
        //was there an error creating the window?
        if (window == 0) return 0;
    
        //display the window
        ShowWindow(window, nCmdShow);
        UpdateWindow(window);
        
        //initialize the game
        if (!Game_Init(window)) return 0;
    
    
        // main message loop
        MSG message;
        while (!gameover)
        {
            if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) 
            {
                TranslateMessage(&message);
                DispatchMessage(&message);
            }
    
            Game_Run(window);
        }
    
        return message.wParam;
    }
    InputHelper.h
    Code:
    #include <string>
    #include <XInput.h>
    #include <vector>
    #include <windows.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <dinput.h>
    #include <time.h>
    #include <iostream>
    using namespace std;
    
    #pragma comment(lib,"d3d9.lib")
    #pragma comment(lib,"d3dx9.lib")
    #pragma comment(lib,"dinput8.lib")
    #pragma comment(lib,"dxguid.lib")
    
    class InputHelper
    {
    public:
    
    	InputHelper(HWND hwnd);
    
    	vector<char> getKeysPressed(HWND hwnd);
    	long *getMouseLocation(long* l);
    
    	~InputHelper(void);
    
    private :
    	LPDIRECTINPUT8 m_dinput;
    	LPDIRECTINPUTDEVICE8 m_diKeyboard;
    	LPDIRECTINPUTDEVICE8 m_diMouse;
    	char m_keys[256];
    	DIMOUSESTATE m_mouseState;
    };
    InputHelper.cpp :
    Code:
    #include <windows.h>
    #include <string>
    #include <XInput.h>
    #include <vector>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <dinput.h>
    #include <time.h>
    #include <iostream>
    #include "InputHelper.h"
    using namespace std;
    
    #pragma comment(lib,"d3d9.lib")
    #pragma comment(lib,"d3dx9.lib")
    #pragma comment(lib,"dinput8.lib")
    #pragma comment(lib,"dxguid.lib")
    #pragma comment(lib,"xinput.lib")
    
    InputHelper::InputHelper(HWND hwnd)
    {
    	//init object
    	HRESULT result = DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&m_dinput,NULL);
    
    	if (SUCCEEDED(result)){
    
    		//get device keybaord
    		result = m_dinput->CreateDevice(GUID_SysKeyboard,&m_diKeyboard,NULL);
    
    		//get device mouse
    		HRESULT result2 = m_dinput->CreateDevice(GUID_SysMouse,&m_diMouse,NULL);
    
    		if (SUCCEEDED(result) && SUCCEEDED(result2)){
    
    			//set format
    			result = m_diKeyboard->SetDataFormat(&c_dfDIKeyboard);
    			result2 = m_diMouse->SetDataFormat(&c_dfDIMouse);
    
    			if (SUCCEEDED(result) && SUCCEEDED(result2)){
    
    				//set cooperative lvl (priority)
    				result = m_diKeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    				result2 = m_diMouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
    
    				if (SUCCEEDED(result) && SUCCEEDED(result2)){
    
    				}
    			}
    		}
    	}
    }
    
    vector<char> InputHelper::getKeysPressed(HWND hwnd)
    {
    	vector<char> keysPressed(1);
    	//get keys
    	if(SUCCEEDED(m_diKeyboard->Acquire()) && SUCCEEDED(m_diKeyboard->GetDeviceState(sizeof(m_keys),(LPVOID)&m_keys)))
    	{
    		int size = sizeof(m_keys);
    		for(DWORD i = 0;i<256;++i)
    		{
    			if(m_keys[i] & 0x80)
    			{
    				MessageBox(hwnd, "PASS","TEST",MB_OK);
    				keysPressed.push_back(m_keys[i]);
    			}
    		}
    	}
    
    	return keysPressed;
    }
    
    long *InputHelper::getMouseLocation(long* l)
    {
    	long *location = l;
    	if(SUCCEEDED(m_diMouse->Acquire()) && SUCCEEDED(m_diMouse->GetDeviceState(sizeof(m_mouseState),(LPVOID)&m_mouseState)))
    	{
    		location[0] = m_mouseState.lX;
    		location[1] = m_mouseState.lY;
    		return location;
    	}
    	else
    	{
    		return location;
    	}
    }
    
    InputHelper::~InputHelper()
    {
    	m_diKeyboard->Unacquire();
    	if(m_diKeyboard)m_diKeyboard->Release();
    	m_diMouse->Unacquire();
    	if(m_diMouse)m_diMouse->Release();
    	if(m_dinput)m_dinput->Release();
    }
    I hope you will be able to help me,

    Best regards,

    rXp>!<


    PS : Is there any way to color the code automatically on the forum ?
    Last edited by rXp; 06-22-2011 at 02:46 AM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    I'm not familiar with DirectX, but this is a lot of code to look through to find a memory access violation. Run your program in debug mode and tell us at which line of execution the program throws.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    rXp
    rXp is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Ho, I did. The line is the one with the //<---------------------------- .
    In the winmain.cpp when I call the getMouseLocation of the InputHelper.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Ah, my mistake. I cannot find the reason for your bug either, sorry. You can color your code with php tags, but in the past it has been frowned upon.
    Code:
    PHP Code:
    int main() {    return 0; } 
    Cheers.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    rXp
    rXp is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    That's too bad, I'm still searching why it does that. Thank you anyway.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Start commenting out code unless you have only the minimum amount of code that causes the problem. Then post that here. Then people will be more willing to help you.
    Also, input is an InputHelper, which is not a pointer. Therefore, it is faulty to initialize it to NULL.
    Another problem is that you are calling input's destructor manually. Do not do this! The destructor should be called by the compiler, and the compiler only!
    Last edited by Elysia; 06-22-2011 at 09:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Have you tried stepping through InputHelper::InputHelper in a debugger?
    You are testing everything for success which is a great start, but you aren't taking any action in case any of the DirectInput functions fail so you might be crashing because you are trying to use pointers to invalid objects.

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Quote Originally Posted by rXp View Post
    Code:
    /**
     ** Game shutdown function
     **/
    void Game_End(HWND hwnd)
    {
    	input.~InputHelper();
    This is not something you do. Ever.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Since you come from Java world, you will find shared pointers very friendly, especially com_ptr, which is essential in native DirectX developement. Shared pointers take role of Java's references. Have a look at: CComPtr Class (ATL)
    I saw some people saying you can use boost's shared_ptr to do the same thing, but personally I find it better to use a dedicated class (which is by the way trivial to implement in case of interfaces).
    Last edited by kmdv; 06-22-2011 at 01:05 PM.
    I never put signature, but I decided to make an exception.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by kmdv View Post
    I saw some people saying you can use boost's shared_ptr to do the same thing, but personally I find it better to use a dedicated class (which is by the way trivial to implement in case of interfaces).
    It can, but people not used to C++ might probably weep at the syntax required to make that work.
    At the very least, you need a bind object or a lambda.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation writing location 0x00000004.
    By zatlas1 in forum C Programming
    Replies: 2
    Last Post: 09-08-2010, 04:12 PM
  2. Access violation reading location
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-24-2010, 03:50 AM
  3. Access violation reading location (Dll and Lib)
    By sarah22 in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2010, 09:09 PM
  4. Replies: 1
    Last Post: 08-13-2008, 03:51 AM
  5. Access violation writing location 0x00000000
    By bennyandthejets in forum Windows Programming
    Replies: 9
    Last Post: 04-14-2004, 04:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21