开发者

Marshal.StructureToPtr crashes Visual Studio

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;

    //.....Implem开发者_开发问答entation 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?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜