Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C#
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;
          }


Here is the structure that cointains szDevLink.

C#
[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;
      }


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.
Posted
Updated 6-Jul-12 4:40am
v2
Comments
Sergey Alexandrovich Kryukov 3-Jul-12 17:18pm    
It all depends on what is ContextP, ContextP.szDevLink and how it was allocated and created. Chances are, you don't need to clear anything, just because you just modify some bytes in some previously existing buffer. There can be a different problem -- you did not pin &ContextP.szDevLink.

To me, the whole idea to use unsafe looks questionable. Why would you do it? However, it depends on the objects you are working with. I don't know enough about your object to give you a final answer.
--SA
nanomass 6-Jul-12 10:35am    
So, by using the fixed keyword I prevent the program from overwriting where my pointer is pointing to. Is there a way to cleat that memory when I am not using it?
Sergey Alexandrovich Kryukov 11-Jul-12 15:04pm    
You do not explicitly clear anything managed. GC calculates reachibility of object handlers (references); and the unreachable object is eventually garbage-collected. Even the call to GC.Collect is just the recommendation; it cannot be imperative.
--SA
nanomass 9-Jul-12 11:59am    
When I tried to use your example, VS says that the expression is already fixed, does this means that this space of memory wont be overwritten by the program?

Thanks in advance.

1 solution

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:
C#
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
                }
            }


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

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900