Adrian H.
Adrian H.

Reputation: 588

Prevent object from being moved by garbage collector in F#

In C# it es easy to pin an object to the place where it currently is stored using the keyword "fixed". Here is an example from MSDN:

unsafe static void TestMethod()
{

    // assume class Point { public int x, y; }
    // pt is a managed variable, subject to garbage collection.
    Point pt = new Point();

    // Using fixed allows the address of pt members to be
    // taken, and "pins" pt so it isn't relocated.

    fixed (int* p = &pt.x)
    {
        *p = 1;
    }        

}

How can this be done in F#?

Upvotes: 3

Views: 624

Answers (1)

desco
desco

Reputation: 16782

you can use GCHandle with type Pinned

Upvotes: 10

Related Questions