gpt4 book ai didi

c# - 崩溃: Attempted to read or write protected memory c#

转载 作者:行者123 更新时间:2023-12-01 23:57:19 27 4
gpt4 key购买 nike

我们的软件经常出现崩溃问题。我想我已经找到了它发生的地方,但我不熟悉代码。调试起来非常困难。

我们无法在我们的开发环境中重现这个问题,它发生在我们的生活环境中。用户使用刷卡登录我们的系统,终端配有磁力刷卡器。当他们刷卡时,这种情况可能每天都会发生一次。 MSR 的读取方式就像键盘一样。

我在不同场合连续刷卡几百次,并且在开发过程中没有出现任何问题。

有 try catch 但它没有捕获任何内容,我们在事件查看器中收到的错误是:

“尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”

这是我很确定它崩溃的函数:

    private void GetRawInputInfo(
IntPtr rawInputHeader,
ref IntPtr deviceHandle,
ref bool handled,
ref StringBuilder buffer)
{
try
{
uint cbSize = 0;
IntPtr hRawInput;

hRawInput = rawInputHeader;


if (UnsafeNativeMethods.GetRawInputData(
hRawInput,
Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
IntPtr.Zero,
ref cbSize,
(uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == 0)
{
IntPtr ptr = Marshal.AllocHGlobal((int)cbSize);

if (ptr != IntPtr.Zero &&
UnsafeNativeMethods.GetRawInputData(
hRawInput,
Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
ptr,
ref cbSize,
(uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == cbSize)
{

Structures.RAWINPUT raw = (Structures.RAWINPUT)Marshal.PtrToStructure(ptr, typeof(Structures.RAWINPUT));

deviceHandle = raw.header.hDevice;
handled = raw.header.dwType == Structures.RAWINPUTDEVICEDWTYPE.RIM_TYPEKEYBOARD &&
raw.keyboard.Message == Messages.WM_KEYDOWN;

if (handled)
{
byte[] state = new byte[256];

// Force the keyboard status cache to update
UnsafeNativeMethods.GetKeyState(0);

// Note: GetKeyboardState only returns valid state when
// the application has focus -- this is why we weren't
// getting shift keys when the application was not focused
if (UnsafeNativeMethods.GetKeyboardState(state))
{
//StringBuilder unmanagedBuffer = new StringBuilder(64);

if (UnsafeNativeMethods.ToUnicode(
raw.keyboard.VKey,
raw.keyboard.MakeCode,
state,
buffer,
64,
0) <= 0)
{
buffer.Remove(0, buffer.Length);
}
}
}
}
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
}
}
}
catch (Exception e)
{
BLog.LogError("Error running: GetRawInputInfo()" + e.Message);
}
}

任何正确方向的帮助或指示(双关语)将不胜感激,

谢谢

----更新:

这里是调用堆栈(如果这有任何帮助的话):

尝试读取或写入 protected 内存。这通常表明其他内存已损坏。

在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32原因,Int32 pvLoopData)

在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)

在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)

在 System.Windows.Forms.Application.RunDialog(Form 表单)

位于 System.Windows.Forms.Form.ShowDialog(IWin32Window 所有者)

在 System.Windows.Forms.Form.ShowDialog()

在 MyApplication.UI.BaseDisplay.ShowDialog(SecurityData security, Int32 listViewState, Nullable`1 TicketId, Int32 currentSeatPosition)

我已经按照马特的要求进行了更改,并将查看崩溃是否有任何变化,但这可能需要一两天的时间。有时它不会每天崩溃,但通常每天都会崩溃。

最佳答案

如果应用程序在 Marshal.AllocHGlobal((int)cbSize) 和 Marshal.FreeHGlobal(ptr) 之间的某个位置抛出异常,您可能会遇到一些内存问题,最终导致应用程序意外崩溃。

为了纠正这种情况,我建议在第一个 if 语句上方声明 IntPtr ptr,然后在 catch block 之后使用 finally block 检查 ptr 变量是否不为 null,然后调用 Marshal.FreeHGlobal(ptr)。

虽然这可能会也可能不会解决您的问题,但最好确保内存始终被解除分配。

代码如下所示

private void GetRawInputInfo(
IntPtr rawInputHeader,
ref IntPtr deviceHandle,
ref bool handled,
ref StringBuilder buffer)
{
try
{
uint cbSize = 0;
IntPtr hRawInput;
IntPtr ptr;

hRawInput = rawInputHeader;


if (UnsafeNativeMethods.GetRawInputData(
hRawInput,
Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
IntPtr.Zero,
ref cbSize,
(uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == 0)
{
ptr = Marshal.AllocHGlobal((int)cbSize);

if (ptr != IntPtr.Zero &&
UnsafeNativeMethods.GetRawInputData(
hRawInput,
Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
ptr,
ref cbSize,
(uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == cbSize)
{

Structures.RAWINPUT raw = (Structures.RAWINPUT)Marshal.PtrToStructure(ptr, typeof(Structures.RAWINPUT));

deviceHandle = raw.header.hDevice;
handled = raw.header.dwType == Structures.RAWINPUTDEVICEDWTYPE.RIM_TYPEKEYBOARD &&
raw.keyboard.Message == Messages.WM_KEYDOWN;

if (handled)
{
byte[] state = new byte[256];

// Force the keyboard status cache to update
UnsafeNativeMethods.GetKeyState(0);

// Note: GetKeyboardState only returns valid state when
// the application has focus -- this is why we weren't
// getting shift keys when the application was not focused
if (UnsafeNativeMethods.GetKeyboardState(state))
{
//StringBuilder unmanagedBuffer = new StringBuilder(64);

if (UnsafeNativeMethods.ToUnicode(
raw.keyboard.VKey,
raw.keyboard.MakeCode,
state,
buffer,
64,
0) <= 0)
{
buffer.Remove(0, buffer.Length);
}
}
}
}
}
}
catch (Exception e)
{
BLog.LogError("Error running: GetRawInputInfo()" + e.Message);
}
finally
{
if (ptr != IntPtr.Zero)
Marshal.FreeHGlobal(ptr);
}
}

关于c# - 崩溃: Attempted to read or write protected memory c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11766666/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com