Jump to content
  • Advertisement
Sign in to follow this  
UncleBob

Crash on acquiring mouse with DirectInput

This topic is 3451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I looked at a lot of tutorials now and tried a lot of stuff, I just can't find my error. I hope someone here will be able to help me out. Here's the deal: On acquiring the mouse (or on trying to check on its state, anyways) I get a crash with the following message:
Quote:
Unhandled exception at 0x0043341f in SpaceEngine.exe: 0xC0000005: Access violation reading location 0x00000000.
I'm doing a class to handle my mouse, and it looks like this: Header:
#pragma once

#include <dinput.h>

class DXInput
{
public:
	DXInput(void);
	~DXInput(void)
	{
		DI_Term();
	}

	void DI_Term();
        HRESULT DI_Init( HWND hDlg );
        DIMOUSESTATE MouseData;
        void GetMouseState();

private:
        LPDIRECTINPUT7           g_pDI;
        LPDIRECTINPUTDEVICE7     g_pMouse;

};
The plan is to call GetMouseState from the gameloop (which is in another sourcefile) and store the values in MouseData. Originaly GetMouseState() was of the type DIMOUSESTATE and would return the value directly, but I seperated it to see if it would solve my problem. Unfortunately it did not. Anyways, here's the code:
#include "DXInputM.h"

DXInput::DXInput(void)
{
}

HRESULT DXInput::DI_Init( HWND hDlg )
{
    HRESULT hr;

    hr = DirectInputCreateEx( g_hInst, DIRECTINPUT_VERSION, IID_IDirectInput7, (void**)&g_pDI, NULL );
    if ( FAILED(hr) ) 
        return hr;

    hr = g_pDI->CreateDeviceEx( GUID_SysMouse, IID_IDirectInputDevice7, (void**)&g_pMouse, NULL );
    if ( FAILED(hr) ) 
        return hr;

    hr = g_pMouse->SetDataFormat( &c_dfDIMouse );
    if ( FAILED(hr) ) 
        return hr;

    hr = g_pMouse->SetCooperativeLevel( hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
	if ( FAILED(hr) ) 
        return hr;

	g_pMouse->Acquire();      //here the acquiring works without crashing

    return S_OK;
}

void DXInput::GetMouseState()
{
	g_pMouse->Acquire();  //and here it goes to hell, when I call the function from the other class
	g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE), (LPVOID)&MouseData);
}


void DXInput::DI_Term()
{
    if (NULL != g_pMouse) 
    {
        g_pMouse->Unacquire();   //if I don't call GetMouseState, everything seems to work fine, but sometimes crashes here on exit

        g_pMouse->Release();
        g_pMouse = NULL;
    }

    if (NULL != g_pDI) 
    {
        g_pDI->Release();
        g_pDI = NULL;
    }
}
With all the tutorials and documentations I consulted I really can't see much difference from what I did to what others did... I'm at a loss here. Help would be greatly apreciated.

Share this post


Link to post
Share on other sites
Advertisement
Upon acquiring, ensure it is not already acquired. Also, don't forget to check for != NULL in GetMouseState() (could be the reason for "Access violation reading location 0x00000000. ", or that violation happens inside Acquire()).

Additionally, ensure you have balanced AddRef()'s and Release()'s for all COM objects.

Share this post


Link to post
Share on other sites
Quote:
Upon acquiring, ensure it is not already acquired.


A first version of my code actually did that... with the result of crashing when he was merely checking. I guess it's a pointer-issue. Unfortunately I'm rather new to C++ and can't handle them properly yet. For example, I have no Idea how to do this:

Quote:
check for != NULL in GetMouseState()


I understand I have a bad pointer. However, I initialized it as I saw in more than one tutorial, or maybe I overlooked just a tiny bit somewhere? I don't think so, but it's possible. So my pointer points to nirvana... any ideas on how to point it the right way?

Share this post


Link to post
Share on other sites
Oh, by that I meant the same like in your line "if (NULL != g_pMouse)", which is btw equivalent to "if (0 != g_pMouse)". Maybe check that?

Share this post


Link to post
Share on other sites
Quote:
Original post by UncleBob
However, I initialized it as I saw in more than one tutorial, or maybe I overlooked just a tiny bit somewhere? I don't think so, but it's possible. So my pointer points to nirvana... any ideas on how to point it the right way?
Just because you did what a tutorial says, doesn't mean it'll always work. This is why you have to check the return values - if some other app has the mouse for exclusive mode, you won't be able to acquire the mouse, or it might fail to create, there's all sorts of things that could go wrong.

As for the problem, if any of the steps in DI_Init() fail, your mouse pointer will be null. If DI_Term() is called at any point before GetMouseState(), your mouse pointer will be null. You can easily check both of these with breakpoints (See Here if you're not familiar with the debugger).

However most importantly, my first point still stands - you really shouldn't use DirectInput. It's deprecated, is a pain in the ass to use, is less performant and far less user friendly and programmer friendly than just using window messages. And window messages are sent anyway, you don't need to spend time setting up for them.
So, why are you using DirectInput for mouse input?

Share this post


Link to post
Share on other sites
Quote:
Oh, by that I meant the same like in your line "if (NULL != g_pMouse)", which is btw equivalent to "if (0 != g_pMouse)". Maybe check that?


oh, that one... :blush:

that comes from copying too much foreign code I don't understand... I learn as I go. Thank you.


Quote:
Original post by Evil Steve
So, why are you using DirectInput for mouse input?


Well, it seemed the most logical joyce to use a library that was designed for games, but I'm currently switching to the API because I just didn't find out what led my pointer astray...

Now I'm facing the problem that WM_MOUSEWHEEL gives me a linker error, but the directX-forum is hardly apropriate for that problem. Plus I still hope to figure it out...

Thanks for the advice!


Share this post


Link to post
Share on other sites
Quote:
Original post by UncleBob
Now I'm facing the problem that WM_MOUSEWHEEL gives me a linker error, but the directX-forum is hardly apropriate for that problem. Plus I still hope to figure it out...


Have you set your version macros correctly? If you don't set those, you're stuck with whatever functionality was available for Windows 95.

Share this post


Link to post
Share on other sites
Yes, I did. before that I got a "unknown identifier", but the solution is quite easy to find with google. Now I'm getting a LNK2019. user32.lib is included, haven't found out yet what I'm lacking. Using Server2003 SDK.

Share this post


Link to post
Share on other sites
Quote:
Original post by UncleBob
Yes, I did. before that I got a "unknown identifier", but the solution is quite easy to find with google. Now I'm getting a LNK2019. user32.lib is included, haven't found out yet what I'm lacking. Using Server2003 SDK.
What's the exact error?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!