在C#中清除指针 [英] Clearing pointers in C#

查看:474
本文介绍了在C#中清除指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的其中一个函数中有一个不安全的代码块,并且有人告诉我垃圾收集器无法清除它.我想知道如何清除那些指针.这是我的源代码.

Hi,

I have a block of unsafe code in one of my functions, and I''ve been told that the garbage collector doesn''t clean that up. I was wondering how I can clear those pointer. Here is my source code.

unsafe
          {
              byte* pointer;
              char[] devicePath = DetailData.DevicePath.ToCharArray();
              Debug.WriteLine(devicePath[0]);
              pointer = &ContextP.szDevLink;
              Debug.WriteLine((int)pointer);
              for (byte loop = 0; loop <= DetailData.DevicePath.Length - 1; loop++)
              {
                  *pointer = (byte)devicePath[loop];
                  pointer++;
              }
              pointer = null;
          }



这是szDevLink的结构.



Here is the structure that cointains szDevLink.

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]
      public struct DFUThreadContextP
      {
          [FieldOffset(0)]
          public Guid DFUGUID;
          [FieldOffset(16)]
          public Guid APPGUID;
          [FieldOffset(32)]
          public UInt32 Operation;
          [FieldOffset(36)]
          public byte bDontSendFFTransfersForUpgrade;
          [FieldOffset(40)]
          public IntPtr hImage;
          [FieldOffset(44)]
          public byte szDevLink;
          [FieldOffset(304)]
          public UInt32 dwTag;
          [FieldOffset(308)]
          public byte percent;
          [FieldOffset(309)]
          public UInt16 wTransferSize;
          [FieldOffset(311)]
          public byte LastDfuStatus_bStatus;
          [FieldOffset(312)]
          public byte LastDfuStatus_bwPollTimeout;
          [FieldOffset(315)]
          public byte LastDfuStatus_bState;
          [FieldOffset(316)]
          public byte LastDfuStatus_iString;
          [FieldOffset(317)]
          public int CurrentRequest;
          [FieldOffset(321)]
          public uint CurrentNBlock;
          [FieldOffset(325)]
          public uint CurrentLenght;
          [FieldOffset(329)]
          public UInt32 CurrentAddress;
          [FieldOffset(333)]
          public uint CurrentImageElement;
          [FieldOffset(337)]
          public UInt32 ErrorCode;
          [FieldOffset(341)]
          public IntPtr hDevice;
      }



我试图以安全模式编写代码,但是如果我没有分配正确的内存并具有C ++数据类型,则正在使用的API将无法正常工作.

我认为可以清除该问题的方法是将指针设置为null,但是我不确定,我还没有在C#中看到有关此主题的可靠文档.

在此先谢谢您.



I tried to write the code in safe mode but the API I am working with wouldn''t work properly if I don''t allocate the right memory and have the C++ data types.

The way I think I can clear it is by setting the pointer to null, however I am not sure and I haven''t seen reliable documentation about this topic in C#.

Thanks in advance.

推荐答案

请参阅我对问题的评论.您有另一个问题,缺少&ContextP.szDevLink的固定可能会导致一些令人讨厌的问题,这些问题很难调试.这样做:
Please see my comment to the question. You have a different problem, lack of pinning of &ContextP.szDevLink which may create some nasty problems which are hard to debug. Do it this way:
unsafe
            {
                //byte* pointer;
                char[] devicePath = DetailData.DevicePath.ToCharArray();
                Debug.WriteLine(devicePath[0]);
                fixed (byte* pointer = &ContextP.szDevLink) {
                    //pointer = &ContextP.szDevLink;
                    Debug.WriteLine((int)pointer);
                    for (byte loop = 0; loop <= DetailData.DevicePath.Length - 1; loop++)
                    {
                        *pointer = (byte)devicePath[loop];
                        pointer++;
                    }
                    //pointer = null; //makes no sense at all
                }
            }



请参阅:
http://msdn.microsoft.com/en-us/library/f58wzh21%28v = vs.100%29.aspx [ ^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/f58wzh21%28v=vs.100%29.aspx[^].

—SA


这篇关于在C#中清除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆