Marshal.StructureToPtr 使 Visual Studio 崩溃

发布于 2024-09-11 11:46:47 字数 744 浏览 2 评论 0原文

我正在开发一个自定义调试引擎,当我将我的结构编组到 IntPtr Visual Studio 崩溃时(正在调试的不是调试器)。

我的结构只不过是:

public struct DocumentContext : IDebugDocumentContext2, IDebugCodeContext2
{
    private string _fileName;

    //.....Implementation of interfaces
}

我的编组代码看起来像这样:

        var documentContext = new DocumentContext(_node.FileName);
        var size = Marshal.SizeOf(documentContext);
        IntPtr ptrDocContext = Marshal.AllocHGlobal(size);
        //This is what is crashing 
        //I don't have a chance to catch anything, it just craps out
        //Event log says faulting dll is nt.dll
        Marshal.StructureToPtr(documentContext, ptrDocContext, true); 

我错过了什么吗?

I'm working on a custom debug engine and when I marshal my structure to a IntPtr Visual Studio crashes (the one being debugged not the debugger).

My struct is little more than:

public struct DocumentContext : IDebugDocumentContext2, IDebugCodeContext2
{
    private string _fileName;

    //.....Implementation of interfaces
}

My marshalling code looks like this:

        var documentContext = new DocumentContext(_node.FileName);
        var size = Marshal.SizeOf(documentContext);
        IntPtr ptrDocContext = Marshal.AllocHGlobal(size);
        //This is what is crashing 
        //I don't have a chance to catch anything, it just craps out
        //Event log says faulting dll is nt.dll
        Marshal.StructureToPtr(documentContext, ptrDocContext, true); 

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-09-18 11:46:47

如果以前从未分配过非托管结构,则不应使用deleteOld。 deleteOld 仅适用于覆盖以前的结构(例如,为了释放字符串引用)。这应该有效:

Marshal.StructureToPtr(documentContext, ptrDocContext, false);

You should not use deleteOld if the unmanaged structure was never previously allocated. deleteOld is only applicable when you're overwriting a previous structure (so as to deallocate string references, for example.) This should work:

Marshal.StructureToPtr(documentContext, ptrDocContext, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文