[Help still] Mogre Access Violation (because of MOIS?)

jintal

27-10-2007 12:45:12

hi guys, i've previously used python-ogre and now i've been playing with mogre for the past month.

The problem is that my app works.. but after 10-20 seconds, access violations occur.

I've closed in on the problematic code. When i take off my input listener, the access violation goes away. but ofcourse, i need the inputs to work.

The code below is the part of my input listener class where the error is.

in a nutshell, the ListenerProcedure method is the one called at the start of every frame. The for each statement below the FInputKeyboard.Capture() is where the problem is. when i comment this out, the problem disappears.


Imports MOIS

Namespace Listeners

''' <summary>
''' This performs the necessary logging of all keyboard and mouse events.
''' </summary>
''' <remarks></remarks>
Public Class InputListener
Inherits BaseListener

Private FInputManager As InputManager
Private FInputMouse As Mouse
Private FInputKeyboard As Keyboard

Private FPreviouslyPressedKeys As New List(Of KeyCode)
Private FPreviouslyClickedButtons As New List(Of MouseButtonID)


' Q-P, A-L, Z-M, 1-0, ESC, F1-F12, SPACE
Private FKeyCodesToListenFor = New Integer()() {New Integer() {16, 25}, _
New Integer() {30, 38}, _
New Integer() {44, 50}, _
New Integer() {2, 11}, _
New Integer() {1, 1}, _
New Integer() {59, 88}, _
New Integer() {57, 57}}

Public Sub New()
Dim plParams As New MOIS.ParamList
Dim ptrWindow As System.IntPtr

MOgreWindow.Singleton.RenderWindow.GetCustomAttribute("WINDOW", ptrWindow)
plParams.Insert("WINDOW", ptrWindow.ToString)

FInputManager = InputManager.CreateInputSystem(plParams)
FInputKeyboard = FInputManager.CreateInputObject(Type.OISKeyboard, False)
FInputMouse = FInputManager.CreateInputObject(Type.OISMouse, False)
FInputKeyboard.SetBuffered(True)
plParams.Dispose()
End Sub

Public Overrides Function ListenerProcedure(ByVal anEvent As FrameEvent) As Boolean
CheckKeyboard()
Return True
End Function

Private Sub CheckKeyboard()
Try

' Capture the keyboard presses
FInputKeyboard.Capture()

' only record a key once it is pressed
For Each intKeyCodes As Integer() In FKeyCodesToListenFor
For i As Integer = intKeyCodes(0) To intKeyCodes(1)
If FInputKeyboard.IsKeyDown(i) And Not FPreviouslyPressedKeys.Contains(i) Then
KeyPresses.Singleton.Append(i)
FPreviouslyPressedKeys.Add(i)
ElseIf Not FInputKeyboard.IsKeyDown(i) Then
FPreviouslyPressedKeys.Remove(i)
End If
Next
Next

Catch ex As Exception

End Try
End Sub

Protected Overrides Sub Finalize()
Try
FInputManager.DestroyInputObject(FInputMouse)
FInputManager.DestroyInputObject(FInputKeyboard)
InputManager.DestroyInputSystem(FInputManager)
Finally
MyBase.Finalize()
End Try
End Sub

End Class

End Namespace


It's weird because the input listener works and i can get the all keypresses...

please help out. i'm kinda stuck with this one.

bleubleu

30-10-2007 00:25:56

Hi there!

I am very suprised that you get a crash. Your code looks very clean, very standard MOIS stuff. I use MOIS all the time and I never had any problems.

The only problem I can see, is that you initialzie MOIS in buffered mode, but you use it as unbuffered. Try setting buffered to false or it you set it to true, use events. Maybe the buffer gets full after a few seconds, I dont know. Just a tought.

You probably know this, but buffered mode means you want to received events every time a key is pressed. So when you set buffered to true, you usually register events like this :

m_joystick = (MOIS.JoyStick)m_manager.CreateInputObject(MOIS.Type.OISJoyStick, true);
m_joystick.SetBuffered(true);
m_joystick.AxisMoved += new JoyStickListener.AxisMovedHandler(m_joystick_AxisMoved);
m_joystick.ButtonPressed += new JoyStickListener.ButtonPressedHandler(m_joystick_ButtonPressed);
m_joystick.ButtonReleased += new JoyStickListener.ButtonReleasedHandler(m_joystick_ButtonReleased);
m_joystick.PovMoved += new JoyStickListener.PovMovedHandler(m_joystick_PovMoved);
m_joystick.SliderMoved += new JoyStickListener.SliderMovedHandler(m_joystick_SliderMoved);


On the other hand, in unbuffered mode, you simple monitor the state of each key/button/axis at discrete time periods (when you call Capture()).

Let me know it it works... That's all I can see wrong with your code.

Mat

jintal

01-11-2007 08:35:15

thanks bleubleu!

i never tried buffered input before, i thought that setting that to true was the only difference. hehe. :D the triggered methods are far better than scanning the keyboard in my opinion. btw, the code i gave was for unbuffered input although i forgot to take off the setBuffered(true).

I took off the unbuffered input code and made all my inputs buffered. that somehow solved my access violation problem. maybe it's a bug?

this is the fixed code (with only the keyboard inputs set up). hope this helps other vb.netters like me



Namespace Listeners

''' <summary>
''' This performs the necessary logging of all keyboard and mouse events.
''' </summary>
''' <remarks></remarks>
Public Class InputListener
Inherits BaseListener

Private FInputManager As InputManager
Private FInputMouse As Mouse
Private FInputKeyboard As Keyboard

' Q-P, A-L, Z-M, 1-0, ESC, F1-F12, SPACE
Private FKeyCodesToListenFor = New Integer()() {New Integer() {16, 25}, _
New Integer() {30, 38}, _
New Integer() {44, 50}, _
New Integer() {2, 11}, _
New Integer() {1, 1}, _
New Integer() {59, 88}, _
New Integer() {57, 57}}

' alt, ctrl, & shift
Private FModifiersToListenFor = New Integer() {MOIS.KeyCode.KC_LSHIFT, _
MOIS.KeyCode.KC_RSHIFT, _
MOIS.KeyCode.KC_LCONTROL, _
MOIS.KeyCode.KC_RCONTROL, _
MOIS.KeyCode.KC_LMENU, _
MOIS.KeyCode.KC_RMENU}

Private FMouseButtonsToListenFor = New Integer() {MouseButtonID.MB_Left, _
MouseButtonID.MB_Middle, _
MouseButtonID.MB_Right}

Public Sub New()
Dim plParams As New MOIS.ParamList
Dim ptrWindow As System.IntPtr

MOgreWindow.Singleton.RenderWindow.GetCustomAttribute("WINDOW", ptrWindow)
plParams.Insert("WINDOW", ptrWindow.ToString)

FInputManager = InputManager.CreateInputSystem(plParams)
FInputKeyboard = FInputManager.CreateInputObject(Type.OISKeyboard, True)
FInputMouse = FInputManager.CreateInputObject(Type.OISMouse, True)
plParams.Dispose()

AddHandler FInputKeyboard.KeyPressed, AddressOf SetKeyPresses
AddHandler FInputKeyboard.KeyPressed, AddressOf SetKeyModifiersPressed
AddHandler FInputKeyboard.KeyReleased, AddressOf SetKeyModifiersReleased

End Sub

Public Overrides Function ListenerProcedure(ByVal anEvent As FrameEvent) As Boolean
CheckKeyboard()
Return True
End Function

Private Function SetKeyPresses(ByVal arg As MOIS.KeyEvent) As Boolean
For Each intKeyCodes As Integer() In FKeyCodesToListenFor
If intKeyCodes(0) <= CInt(arg.key) And CInt(arg.key) <= intKeyCodes(1) Then
KeyPresses.Singleton.Append(CType(arg.key, MOIS.KeyCode))
Return True
End If
Next
Return True
End Function

Private Function SetKeyModifiersPressed(ByVal arg As MOIS.KeyEvent) As Boolean
If Array.IndexOf(FModifiersToListenFor, CInt(arg.key)) > -1 Then KeyModifiersDown.Singleton.Append(CType(arg.key, MOIS.KeyCode))
Return True
End Function

Private Function SetKeyModifiersReleased(ByVal arg As MOIS.KeyEvent) As Boolean
If KeyModifiersDown.Singleton.Items.IndexOf(CType(arg.key, MOIS.KeyCode)) > -1 Then KeyModifiersDown.Singleton.Remove(CType(arg.key, MOIS.KeyCode))
Return True
End Function

Private Sub CheckKeyboard()

' Capture the keyboard presses
FInputKeyboard.Capture()

End Sub

Protected Overrides Sub Finalize()
Try
FInputManager.DestroyInputObject(FInputMouse)
FInputManager.DestroyInputObject(FInputKeyboard)
InputManager.DestroyInputSystem(FInputManager)
Finally
MyBase.Finalize()
End Try
End Sub

End Class

End Namespace

bleubleu

01-11-2007 19:08:03

Cool! Glad it worked for you!

Mat

jintal

02-11-2007 16:31:07

the problem's back!! i keep getting these access violations!

i just keep getting this after a few seconds of runtime..

An unhandled exception of type 'System.AccessViolationException' occurred in Mogre.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


is there a way to properly catch errors?

i can't seem to catch any and breakpoints don't work. I've put the OgreMain_d object file library (and others too) in my debug folder :?:

bleubleu

02-11-2007 22:10:39

If breakpoints dont work, it is because you dont have Mogre.pdb. Look for it somewhere. If you dont have it, it means you have some bastard release.

Could you post the code for your listener, and a short usage example to make it crash. I will test it here for you.

Mat