Click here to Skip to main content
6,292,426 members and growing! (9,845 online)
Email Password   helpLost your password?
Languages » C# » Memory Management     Intermediate License: The Code Project Open License (CPOL)

Pinned Object

By Marc Clifton

A helper class for working with structures that need to be pinned to prevent the GC from moving them.
C# 2.0, .NET, Win2K, WinXP, Win2003, VistaVS2005, Dev
Posted:11 Feb 2007
Views:15,451
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 5.16 Rating: 4.50 out of 5

1
2 votes, 14.3%
2

3
2 votes, 14.3%
4
10 votes, 71.4%
5

Introduction

This article describes a very short helper class to work with unmanaged memory that must be pinned to prevent the garbage collector from moving it. This is a particular requirement when working with the Win32 API's asynchronous I/O methods, which I found myself doing when implementing a USB device interface.

There is no download; simply copy the code from this article--please do not remove the copyright.

The Code

There really isn't much to this code:

  • It uses generics to internally cast the unmanaged memory to the desired managed type.
  • The class maintains an instance of the managed structure.
  • The constructor pins this structure and initializes a pointer suitable for working with Win32 API methods.
  • The ManagedObject property is used to return a managed object that references the unmanaged memory and to copy the managed object to the unmanaged memory. One caveat is that the managed object must be a structure suitable for use with the Marshal class.
  • The Pointer property is used to return the address of the unmanaged memory.
  • The destructor disposes of the unmanaged memory.
  • The class implements IDisposable, so that you can use this class in a "using" block as well.

Here's the code:

// (c) 2007 Marc Clifton

using System;
using System.Runtime.InteropServices;

namespace Clifton.Tools.Interop
{
  /// <summary>

  /// A helper class for pinning a managed structure so that it is suitable for

  /// unmanaged calls. A pinned object will not be collected and will not be moved

  /// by the GC until explicitly freed.

  /// </summary>

  public class PinnedObject<T> : IDisposable where T : struct
  {
    protected T managedObject;
    protected GCHandle handle;
    protected IntPtr ptr;
    protected bool disposed;

    public T ManangedObject
    {
      get 
      {
        return (T)handle.Target;
      }
      set
      {
        Marshal.StructureToPtr(value, ptr, false);
      }
    }

    public IntPtr Pointer
    {
      get { return ptr; }
    }

    public PinnedObject()
    {
      handle = GCHandle.Alloc(managedObject, GCHandleType.Pinned);
      ptr = handle.AddrOfPinnedObject();
    }

    ~PinnedObject()
    {
      Dispose();
    }

    public void Dispose()
    {
      if (!disposed)
      {
        handle.Free();
        ptr = IntPtr.Zero;
        disposed = true;
      }
    }
  }
}

Example

Using Unsafe Pointers In C#

This example illustrates manipulating a PinnedObject using C++ style pointer syntax within an unsafe code block. The code illustrates:

  • Assigning a new structure to the pinned object (testing the ManagedObject setter)
  • Getting the pointer and manipulating the structure via the pointer (testing the Pointer getter)
  • Getting the structure (testing the ManagedObject getter)
using System;

using Clifton.Tools.Interop;

namespace pintest
{
  public struct TestStruct
  {
    public int a;
  }

  public static class Test
  {
    public static void Main()
    {
      PinnedObject<TestStruct> pin = new PinnedObject<TestStruct>();
      TestStruct ts = new TestStruct();
      ts.a = 1;
      pin.ManangedObject = ts;

      unsafe
      {
        TestStruct* p = (TestStruct*)pin.Pointer;
        ++p->a;
      }

    Console.WriteLine(pin.ManangedObject.a);
    }
  }
}

Conclusion

A simple class but hopefully you will find it useful for the strong type management and functionality that it encapsulates.

License

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

About the Author

Marc Clifton


Member
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles.

Marc lives in Hudson, NY with his son Ian, who attends the Hawthorne Valley School. To contact Marc, email him at marc.clifton@gmail.com.
Occupation: Architect
Company: Interacx
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralPinning copy of the structure PinmemberLesheniuk6:04 2 Mar '07  
GeneralRe: Pinning copy of the structure PinsupporterMarc Clifton6:18 2 Mar '07  
Questionfixed statement Pinmemberrvpilot11:48 13 Feb '07  
AnswerRe: fixed statement PinsupporterMarc Clifton12:06 13 Feb '07  
GeneralRe: fixed statement Pinmemberrvpilot12:14 13 Feb '07  
GeneralTesting PinmemberLuc Pattyn13:07 12 Feb '07  
GeneralRe: Testing PinsupporterMarc Clifton14:28 12 Feb '07  
GeneralMy .NET 1.1 approach, without generics PinmemberLuc Pattyn12:27 12 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Feb 2007
Editor: J. Dunlap
Copyright 2007 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project