Skip to content

Font byte[] constructor #156

@FRex

Description

@FRex

There is strong suspicion we use a pin, take address of a memory block and then unpin the block, so if we read from it much later it'll fail if the GC has moved it.

The offending code is in src/Graphics/Font.cs:

public Font(byte[] bytes) : base(IntPtr.Zero)
{
    GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    try
    {
        CPointer = sfFont_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length));
    }
    finally
    {
        pin.Free();
    }
    if (CPointer == IntPtr.Zero)
    {
        throw new LoadingFailedException("font");
    }
}

Docs of this functionality: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.gchandletype?view=netframework-4.7.2

Came up in this thread (and supposedly using a MemoryStream instead works as fix without requiring changes to C/C++ side): https://en.sfml-dev.org/forums/index.php?topic=24650

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions