The object is pinned during the fixed block only. It ceases to become
pinned right after the pinned block is executed.
Here is how it works. Although the variable pinning the object is live
during the whole method, it is filled with null right after the fixed
block is executed. You can see it from following example - IL_0026 and
IL_0027 is where the variable is filled with null.
using System;
class My {
unsafe static void Main() {
Console.WriteLine("Before fixed");
fixed (char* c = new String('c', 1)) {
Console.WriteLine("Inside fixed");
}
Console.WriteLine("After fixed");
}
}
is translated into:
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 51 (0x33)
.maxstack 3
.locals (char* V_0,
string pinned V_1)
IL_0000: ldstr "Before fixed"
IL_0005: call void
[mscorlib]System.Console::WriteLine(string)
IL_000a: ldc.i4.s 99
IL_000c: ldc.i4.1
IL_000d: newobj instance void
[mscorlib]System.String::.ctor(char,
int32)
IL_0012: stloc.1
IL_0013: ldloc.1
IL_0014: conv.i
IL_0015: call int32
[mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::get_OffsetToSt
ringData()
IL_001a: add
IL_001b: stloc.0
IL_001c: ldstr "Inside fixed"
IL_0021: call void
[mscorlib]System.Console::WriteLine(string)
IL_0026: ldnull
IL_0027: stloc.1
IL_0028: ldstr "After fixed"
IL_002d: call void
[mscorlib]System.Console::WriteLine(string)
IL_0032: ret
} // end of method My::Main
-Jan
This posting is provided "AS IS" with no warranties, and confers no
rights.
-----Original Message-----
From: Discussion of the Rotor Shared Source CLI implementation
[mailto:[EMAIL PROTECTED] On Behalf Of Archana
Sent: Sunday, June 08, 2003 2:19 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET-ROTOR] scope of pinned variables
Hi,
Thank you for this information, but what i wanted to know is-
suppose a method has a fixed block meant to pin an object say X,
does X stay pinned throughout the lifetime of the method or does
it cease to become pinned right after the fixed block is executed?
a sample dis-assembled program seemed to indicate that, the fixed
objects were prefixed with 'pinned' at the beginning of the method
declaration.
so does that mean X ceases to be pinned only after it goes out of scope?
regards
On Fri, 6 Jun 2003, Jan Kotas wrote:
> The pinning bit is used only within the GC when everything else is
> stopped. It should be never set on a live object when the managed code
> is running. The bit is set in the scan phase and reset in the copy
phase
> (in scavenge). Look for clear_marked_pinned in gcsmp.cpp.
>
> -Jan
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> -----Original Message-----
> From: Discussion of the Rotor Shared Source CLI implementation
> [mailto:[EMAIL PROTECTED] On Behalf Of Archana
> Sent: Friday, June 06, 2003 6:00 AM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET-ROTOR] scope of pinned variables
>
> Hi,
> this is wrt implementation of status updation of an object.
> when is the pin-bit of an object reset?. ie. when does the GC get the
> information that, okay now this particular object X is no longer
pinned.
> the GC appears to recognize pinning/set the pin bit thru pinObject in
> Promote.
>
> Thanks,
> archana
>