I'm getting the following error with unmanaged code in .net 2.0 using the dx 2006 managed references (I can not find anything newer that has a managed code folder).
An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.DirectX.DirectInput.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I was wondering how to figure out what memory I may have corrupted as this is pretty much a one for one from example code. Though to be sure here is the code that fails.
Effect effect;
EffectObject eo = null;
public void doFFBAPI(Device joystick)
{
// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects( EffectType.All );
//Configure axes
int[] axis = null;
foreach (DeviceObjectInstance doi in joystick.Objects)
{
//Set axes ranges.
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(
ParameterHow.ById,
doi.ObjectId,
new InputRange(-5000, 5000));
}
int[] temp;
// Get info about first two FF axii on the device
if ( ( doi.Flags & (int)ObjectInstanceFlags.Actuator ) != 0 )
{
if ( axis != null )
{
temp = new int[axis.Length + 1];
axis.CopyTo( temp, 0 );
axis = temp;
}
else
{
axis = new int[1];
}
// Store the offset of each axis.
axis[axis.Length - 1] = doi.Offset;
if ( axis.Length == 2 )
{
break;
}
}
}
foreach (EffectInformation ei in joystick.GetEffects(EffectType.CustomForce))
{
// Fill in some generic values for the effect.
effect = new Effect();
effect.SetDirection(new int[axis.Length]);
effect.SetAxes(new int[1]); //.SetAxes(new int[axis.Length]); ->may cause invaild range error?
effect.ConditionStruct = new Condition[axis.Length];
effect.EffectType = EffectType.CustomForce;
effect.ConditionStruct = new Condition[axis.Length];
effect.Duration = (int)DI.Infinite;
effect.Constant = new ConstantForce();
effect.Constant.Magnitude = 100;
effect.Gain = 10000;
effect.SamplePeriod = 0;
effect.TriggerButton = (int)Microsoft.DirectX.DirectInput.Button.NoTrigger;
effect.TriggerRepeatInterval = (int)DI.Infinite;
effect.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
effect.UsesEnvelope = false;
Periodic p = new Periodic();
p.Magnitude = 10;
p.Offset = 10;
p.Period = 10;
p.Phase = 10;
effect.Periodic = p;
int[] forces;
forces = new int[24];
forces[0] = 79;
forces[1] = 1;
forces[2] = 1;
forces[3] = 1;
forces[4] = 1;
forces[5] = 1;
forces[6] = 1;
forces[7] = 1;
forces[8] = 1;
forces[9] = 1;
forces[10] = 1;
forces[11] = 1;
forces[12] = 1;
forces[13] = 1;
forces[14] = 1;
forces[15] = 1;
forces[16] = 1;
forces[17] = 1;
forces[18] = 1;
forces[19] = 1;
forces[20] = 1;
forces[21] = 1;
forces[22] = 1;
forces[23] = 1;
CustomForce CF = new CustomForce();
CF.Channels = 2;
CF.Samples = 12;
CF.SamplePeriod = 1000;
CF.SetForceData(forces);
effect.CustomStruct = CF;
// Create the effect, using the passed in guid.
eo = new EffectObject(ei.EffectGuid, effect, joystick);
//eo.SetParameters(effect, EffectParameterFlags.Start);
}
}
I'm not sure any other code would be relevant but ill post it if needed. If I change my effect object constructor to use no effect (eo = new EffectObject(ei.EffectGuid, joystick);) it does so without the error but when adding the effect via SetParameters the error is thrown. From what I have read on this error the hard part is tracking down the DLL that is causing the error but in this case I know it is directinput. So from here what can I do to figure out the memory leak or corruption?
Not sure a call stack would help in this case but here it is:
[Managed to Native Transition]
Microsoft.DirectX.DirectInput.dll!Microsoft.DirectX.DirectInput.EffectObject.InternalCreate(System.Guid eff, ref Microsoft.DirectX.DirectInput.Effect die, Microsoft.DirectX.DirectInput.Device dev) + 0xd7 bytes
Microsoft.DirectX.DirectInput.dll!Microsoft.DirectX.DirectInput.EffectObject.EffectObject(System.Guid eff, Microsoft.DirectX.DirectInput.Effect die, Microsoft.DirectX.DirectInput.Device dev) + 0x4d bytes
> Bliss-BoxAPI.exe!BlissBox.DXinput.doFFBAPI(Microsoft.DirectX.DirectInput.Device joystick) Line 162 + 0xbd bytes C#
Bliss-BoxAPI.exe!BlissBox.BBapi.Main() Line 28 + 0xd bytes C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes