Jump to content
  • Advertisement
Sign in to follow this  
Steve-B

Very strange problem which seems related to DirectInput...

This topic is 5098 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

Hi guys Ever since I added DirectInput code to my program (about a year ago) every now and again my program crashes, seemingly randomly. The error I recieved was the standard XP 'Please tell Microsoft about this problem' message. If I click on debug I am given a memory location and that a 'memory write error occured'. The crash can occur after a minute or two and I have successfully ran my program for hours before it crashes with the same problem. I'd like to know if there is something you guys can see that I'm doing wrong in my DirectInput code. I've done some testing and it doesn't appear to crash when I remove the DirectInput function calls. I think I might have tracked the bug down to the UpdateKeyboard function but I'm not 100% on this. UpdateMouse and UpdateJoystick seem to be fairly stable. Here is my the code in DirectInput.cpp:

// Call this function in WinMain to update the keyboard state in real-time
bool DirectInput::UpdateKeyboard(LPVOID keyboard_state)
{
	hr = lpdiKeyboard->GetDeviceState(sizeof(UCHAR[256]), keyboard_state);
	
	if(FAILED(hr))
	{
		if(hr == DIERR_INPUTLOST)
		{
			// The device has been lost so we attempt to reacquire it...
			hr = lpdiKeyboard->Acquire();
			if(FAILED(hr)) return FALSE;
		}
	}

	return TRUE;
}


// Call this function in WinMain to update the mouse state in real-time
bool DirectInput::UpdateMouse(DIMOUSESTATE2 *mouse_state)
{
	hr = lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE2), mouse_state);

	if(FAILED(hr))
	{
		if(hr == DIERR_INPUTLOST)
		{
			// The device has been lost so we attempt to reacquire it...
			hr = lpdiMouse->Acquire();
			if(FAILED(hr)) return FALSE;
		}
	}

	return TRUE;
}


// Call this function in WinMain to update the joystick state in real-time
bool DirectInput::UpdateJoystick(DIJOYSTATE2 *joystick_state)
{
	if(joystick_found)
	{
		hr = lpdiJoystick->Poll();		// Poll is only required for joysticks!

		if(FAILED(hr))
		{
			if(hr == DIERR_INPUTLOST)
			{
				// The device has been lost so we attempt to reacquire it...
				hr = lpdiJoystick->Acquire();
				if(FAILED(hr)) return FALSE;
			}
		}
		
		hr = lpdiJoystick->GetDeviceState(sizeof(DIJOYSTATE2), joystick_state);
		if(FAILED(hr)) return FALSE;
	}
	
	return TRUE;
}

Share this post


Link to post
Share on other sites
Advertisement
This is what my main loop looks like. It constantly reads the input devices every frame and saves the information in the structures/array. It all seems fine to me but I'm sure something must be wrong somewhere. If not then I guess the crash must be down to a bad graphics driver. Note that the devices do actually work and I can successfully read data from the input devices. Also I have put these three stuctures/array as externs in my Main.h header file so they are accessible to other files within my program. This couldn't be the cause of the problem could it?

Anyway here is the code for my program main loop:

Hope someone can help,
Steve-B


UCHAR key_state[256]; // Create our array for storing the keyboard state
DIMOUSESTATE2 mouse_state; // Create a pointer to the mouse state structure
DIJOYSTATE2 joy_state; // Create a pointer to the joystick state structure



// THE MAIN WINDOWS EVENT LOOP
while(TRUE)
{
if(!r3d->ProcessMessages()) // Quit if the function returns FALSE
break;

dinput->UpdateKeyboard(key_state); // Check the keyboard for key presses
dinput->UpdateMouse(&mouse_state); // Check the mouse for clicks/movements
dinput->UpdateJoystick(&joy_state); // Check the joystick for any actions

Update(timer->elapsed); // Update the display
Render(timer->clock); // Render the display

SwapBuffers(r3d->hDC); // Swap the buffers (Double Buffering)

timer->UpdateTimer(); // Update the current timer (measured in seconds)
timer->UpdateFrame(); // Update the frame rate (measured in frames per second)
}



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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!