我使用dx 2006托管引用在.net 2.0中的非托管代码中出现以下错误(我找不到任何具有托管代码文件夹的新版本)。
未处理的类型' System.AccessViolationException' 发生在Microsoft.DirectX.DirectInput.dll
中附加信息:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。
我想知道如何弄清楚我可能已经损坏了什么内存,因为这几乎是来自示例代码的一对一。虽然这里确定代码失败了。
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);
}
}
我不确定任何其他代码是否相关,但如果需要则会发布。如果我改变我的效果对象构造函数以使用无效(eo = new EffectObject(ei.EffectGuid,joystick);)它没有错误,但是当通过SetParameters添加效果时,会抛出错误。从我读到的关于这个错误的内容来看,困难的部分是追踪导致错误的DLL,但在这种情况下我知道它是直接输入。那么从这里我可以做些什么来弄清楚内存泄漏或损坏?
在这种情况下,不确定调用堆栈是否会有所帮助,但这里是:
[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