Export (0) Print
Expand All

FixedAddressValueTypeAttribute Class

Updated: May 2010

Fixes the address of a static value type field throughout its lifetime. This class cannot be inherited.

Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets.Field)]
public sealed class FixedAddressValueTypeAttribute : Attribute

Use the FixedAddressValueTypeAttribute attribute to mark static value types as pinned upon creation.

This attribute is used by the Microsoft Visual C++ compiler.

Static value type fields are created as boxed objects. This means that their address can change as garbage collection is performed. When you apply this attribute to a static value type, its address remains constant during its lifetime.

The following example illustrates the use of the FixedAddressValueTypeAttribute attribute to pin a static field in memory. It defines an Age structure and initializes two classes that have static fields of type Age. The second class applies FixedAddressValueTypeAttribute to pin the field's address. A number of memory allocations are made before and after these two objects are instantiated and the garbage collector is invoked. The output from the example shows that although the address of the first Age field has changed after garbage collection, the address of the field to which FixedAddressValueTypeAttribute is applied has not.

using System;
using System.Runtime.CompilerServices;

public struct Age {
   public int years;
   public int months;
}

public class FreeClass
{
   public static Age FreeAge;

   public static unsafe IntPtr AddressOfFreeAge()
   { 
      fixed (Age* pointer = &FreeAge) 
      { return (IntPtr) pointer; } 
   }
}

public class FixedClass
{
   [FixedAddressValueType]
   public static Age FixedAge;

   public static unsafe IntPtr AddressOfFixedAge()
   { 
      fixed (Age* pointer = &FixedAge) 
      { return (IntPtr) pointer; } 
   }   
}

public class Example
{
   public static void Main()
   {
      AllocateMemory();

      // Get addresses of static Age fields.
      IntPtr freePtr1 = FreeClass.AddressOfFreeAge();
      AllocateMemory();

      IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();
      AllocateMemory();

      // Garbage collection.
      GC.Collect();
      GC.WaitForPendingFinalizers();

      // Get addresses of static Age fields after garbage collection.
      IntPtr freePtr2 = FreeClass.AddressOfFreeAge();
      IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();

      // Display addresses before and after garbage collection
      Console.WriteLine("Normal static: {0} -> {1}", freePtr1, freePtr2);
      Console.WriteLine("Pinned static:  {0} -> {1}", fixedPtr1, fixedPtr2);  
   }

   // Allocate memory for 100,000 objects. 
   static public void AllocateMemory()
   {
      for (int ctr = 0; ctr <= 100000; ctr++)
      {
         object o = new object();      
      }
   }
}
// The example displays output similar to the following: 
//       Normal static: 19932420 -> 19863704 
//       Pinned static:  19985508 -> 19985508

System.Object
  System.Attribute
    System.Runtime.CompilerServices.FixedAddressValueTypeAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

May 2010

Replaced the example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft