gpt4 book ai didi

具有非托管 DLL 的 C# 应用程序卡住整个系统

转载 作者:太空狗 更新时间:2023-10-29 23:47:01 24 4
gpt4 key购买 nike

我目前正在 Visual Studio 2012 中编写用于与 RFID 卡通信的软件。我有一个用 Delphi 编写的 DLL 来处理与读卡器的通信。

问题是:我的软件在安装了 VS2012 的机器上运行良好。在其他系统上,它会卡住自身或整个系统。我在具有 x32 和 x64 配置的 Win XP/7/8 上进行了尝试。我正在使用 .NET 4.0。

连接到读卡器后,软件会启动一个 backgroundWorker,它会(以 200 毫秒的速率)轮询读卡器,并发出命令以清点读卡器射频场中的卡片。崩溃通常发生在 ca。读卡器连接后 10 到 20 秒。这是代码:

[DllImport("tempConnect.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int inventory(int maxlen, [In] ref int count,
IntPtr UIDs, UInt32 HFOffTime);
public String getCardID()
{
if (isConnectet())
{
IntPtr UIDs = IntPtr.Zero;
int len = 2 * 8;
Byte[] zero = new Byte[len];
UIDs = Marshal.AllocHGlobal(len);
Thread.Sleep(50);
Marshal.Copy(zero, 0, UIDs, len);
int count = 0;
int erg;
String ret;
try
{
erg = inventory(len, ref count, UIDs, 50);
}
catch (ExternalException) // this doesn't catch anything (iI have set <legacyCorruptedStateExceptionsPolicy enabled="true"/>)
{
return "\0";
}
finally
{
ret = Marshal.PtrToStringAnsi(UIDs, len);
IntPtr rslt = LocalFree(UIDs);
GC.Collect();
}
if (erg == 0)
return ret;
else
return zero.ToString();
}
else
return "\0";
}

该DLL是用Delphi编写的,代码DLL命令为:

function inventory (maxlen: Integer; var count: Integer; 
UIDs: PByteArray; HFOffTime: Cardinal = 50): Integer; STDCALL;

我认为某处可能存在内存泄漏,但我不知道如何找到它...


编辑:

我在上面的代码中添加了一些想法(显式 GC.Collect()、try-catch-finally),但它仍然不起作用。

这是调用 getCardID() 的代码:

每 200 毫秒运行一次的操作:

if (!bgw_inventory.IsBusy)
bgw_inventory.RunWorkerAsync();

Async backgroundWorker 做:

private void bgw_inventory_DoWork(object sender, DoWorkEventArgs e)
{
if (bgw_inventory.CancellationPending)
{
e.Cancel = true;
return;
}
else
{
String UID = reader.getCardID();
if (bgw_inventory.CancellationPending)
{
e.Cancel = true;
return;
}
if (UID.Length == 16 && UID.IndexOf("\0") == -1)
{
setCardId(UID);
if (!allCards.ContainsKey(UID))
{
allCards.Add(UID, new Card(UID));
}
if (readCardActive || deActivateCardActive || activateCardActive)
{
if (lastActionCard != UID)
actionCard = UID;
else
setWorkingStatus("OK", Color.FromArgb(203, 218, 138));
}
}
else
{
setCardId("none");
if (readCardActive || deActivateCardActive || activateCardActive)
setWorkingStatus("waiting for next card", Color.Yellow);
}
}
}

编辑

到目前为止,我已经对代码进行了一些小的修改(上面的更新)。现在只有应用程序。在“tempConnect.dll”处因 0xC00000FD(堆栈溢出)而崩溃。 这不会发生在安装了 VS2012 的系统上,或者如果我使用原生 Delphi 的 DLL!有没有人有任何其他想法?


编辑

现在我制作了 DLL 日志记录它的堆栈大小并发现了一些奇怪的东西:如果它从我的 C# 程序中调用和轮询,则堆栈大小会不断上下变化。如果我从一个自然的 Deplhi 程序做同样的事情,堆栈大小是不变的!所以我会做进一步的调查,但我真的不知道我必须搜索什么......

最佳答案

我有点担心如何使用 Marshal 对象。正如您担心内存泄漏一样,它似乎经常分配内存,但我没有看到它显式释放它。垃圾收集器应该(操作词)负责处理这个问题,但您自己说您混合了一些非托管代码。发布的信息很难判断非托管代码从哪里开始。

查看 this question对于一些在 .NET 本身中查找内存泄漏的好技术 - 这将为您提供大量有关代码的托管端(即您可以直接控制的部分)如何使用内存的信息。使用带有断点的 Windows 性能监视器来关注系统的整体健康状况。如果 .NET 看起来正常,但 WPM 显示一些尖峰,则它可能在非托管代码中。除了您在那里的使用之外,您无法真正控制任何东西,因此可能是时候返回文档了。

关于具有非托管 DLL 的 C# 应用程序卡住整个系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182913/

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