none
Fixing a pinned object RRS feed

  • Question

  • Hello,
    I need to perform an operation on one byte array and store it in another byte array. Normally I would use fixed (byte* pSource = source, pDestination = destination), however, this time I know the source array is already pinned using GCHandle.Alloc(source, pinned) for other purposes.

    So, first question is whether using the fixed keyword on pinned objects somehow influences their pinned state, like cancels it before or after the fixed block.

    If no, should I just go with fixing the source "again" or is it better to use sourceHandle.AddrOfPinnedObject().ToPointer()?

    Any insight would be welcomed.

    Thanks!
    Jan

    Thursday, February 24, 2011 12:40 PM

Answers

  •  

    The fixed keyword will not affect object which pinned by GCHandle.Alloc method; GCHandle.Alloc internally allocate an entry in GC Handle Table for the pinned object, the fixed keyword will neither allocate duplicate entry when entering the fixed context, nor remove the entry when leaving the fixed context.

     

    More test shows that if we do not use GCHandle.Alloc method, just use fixed keyword, when entering the fixed context, there is no entry created in the GC Handle Table, it seems to me that fixed keyword use different data structure to pin an object.

     

    However, regardless whatever the fixed keyword used to pin an object, there will some overhead, so,

    sourceHandle.AddrOfPinnedObject().ToPointer() is enough for object which has been pinned.

     

    My test is based on Visual Studio Debugger + SOS, e.g. !gcroot command.

    See also: How to:

    Use SOS

    SOS.dll (SOS Debugging Extension)


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, March 7, 2011 3:47 AM
    Monday, February 28, 2011 10:12 AM

All replies

  • Hello,
    I need to perform an operation on one byte array and store it in another byte array. Normally I would use fixed (byte* pSource = source, pDestination = destination), however, this time I know the source array is already pinned using GCHandle.Alloc(source, pinned) for other purposes.

    So, first question is whether using the fixed keyword on pinned objects somehow influences their pinned state, like cancels it before or after the fixed block.

    If no, should I just go with fixing the source "again" or is it better to use sourceHandle.AddrOfPinnedObject().ToPointer()?

    Any insight would be welcomed.

    Thanks!
    Jan


    I can't offer an answer here, but I am interested in hearing one.

    I am still very unclear of exactly how the underlying MSIL pins, it seems there is no "pin" instruction in MSIL, only the ability to declare a pinned ref (&) or a pinned ptr (*).

    Have a look at this thread which may be of interest to you

    Cap'n

     

     

    Friday, February 25, 2011 5:02 PM
  •  

    The fixed keyword will not affect object which pinned by GCHandle.Alloc method; GCHandle.Alloc internally allocate an entry in GC Handle Table for the pinned object, the fixed keyword will neither allocate duplicate entry when entering the fixed context, nor remove the entry when leaving the fixed context.

     

    More test shows that if we do not use GCHandle.Alloc method, just use fixed keyword, when entering the fixed context, there is no entry created in the GC Handle Table, it seems to me that fixed keyword use different data structure to pin an object.

     

    However, regardless whatever the fixed keyword used to pin an object, there will some overhead, so,

    sourceHandle.AddrOfPinnedObject().ToPointer() is enough for object which has been pinned.

     

    My test is based on Visual Studio Debugger + SOS, e.g. !gcroot command.

    See also: How to:

    Use SOS

    SOS.dll (SOS Debugging Extension)


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, March 7, 2011 3:47 AM
    Monday, February 28, 2011 10:12 AM