开发者

Prevent object from being moved by garbage collector in F#

开发者 https://www.devze.com 2023-02-09 18:36 出处:网络
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:

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;
    }  开发者_Python百科      

}

How can this be done in F#?


you can use GCHandle with type Pinned

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号