Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Segfault on SteamNetworkingMessage_t.Release() #424

Closed
fghl opened this issue Jun 3, 2021 · 5 comments
Closed

Segfault on SteamNetworkingMessage_t.Release() #424

fghl opened this issue Jun 3, 2021 · 5 comments

Comments

@fghl
Copy link

fghl commented Jun 3, 2021

Hi there,

I'm running the latest (giturl) install of Steamworks.NET and am sending and receiving messages just fine it seems.

However when I call the recommended Release method on a SteamNetworkingMessage_t, I get a segfault.

My code is here:

        try
        {
          SteamNetworkingMessage_t netMessage = Marshal.PtrToStructure<SteamNetworkingMessage_t>(receiveBuffers[0]);
          byte[] message = new byte[netMessage.m_cbSize];
          Marshal.Copy(netMessage.m_pData, message, 0, message.Length);


          payload = new ArraySegment<byte>(message);
          clientId = netMessage.m_identityPeer.GetSteamID64();

          Debug.Log($"PollEvent(); // message received at {receiveTime} from {clientId}");

          netMessage.Release(); // <----- Commenting this line prevents segfault
        }
        finally
        {
          Marshal.DestroyStructure<SteamNetworkingMessage_t>(receiveBuffers[0]);
        }

Any ideas? Happy to share more code if needed.

@fghl fghl changed the title Segfault on calling SteamNetworkingMessage_t.Release() Segfault on SteamNetworkingMessage_t.Release() Jun 3, 2021
@rlabrecque
Copy link
Owner

Are these helpful?

#421

#388

@AG4W
Copy link

AG4W commented Jun 4, 2021

Hi there,

I'm running the latest (giturl) install of Steamworks.NET and am sending and receiving messages just fine it seems.

However when I call the recommended Release method on a SteamNetworkingMessage_t, I get a segfault.

My code is here:

        try
        {
          SteamNetworkingMessage_t netMessage = Marshal.PtrToStructure<SteamNetworkingMessage_t>(receiveBuffers[0]);
          byte[] message = new byte[netMessage.m_cbSize];
          Marshal.Copy(netMessage.m_pData, message, 0, message.Length);


          payload = new ArraySegment<byte>(message);
          clientId = netMessage.m_identityPeer.GetSteamID64();

          Debug.Log($"PollEvent(); // message received at {receiveTime} from {clientId}");

          netMessage.Release(); // <----- Commenting this line prevents segfault
        }
        finally
        {
          Marshal.DestroyStructure<SteamNetworkingMessage_t>(receiveBuffers[0]);
        }

Any ideas? Happy to share more code if needed.

You can wrangle NativeMethods in this way to expose the underlying method (internal to public) and call it through the API.

[DllImport(NativeLibraryName, EntryPoint = "SteamAPI_SteamNetworkingMessage_t_Release", CallingConvention = CallingConvention.Cdecl)]
public static extern void SteamAPI_SteamNetworkingMessage_t_Release(IntPtr self);

I use some extension methods to do it.

public static class MessageExtensions
{
    public static T To<T>(this IntPtr pointer) => Marshal.PtrToStructure<T>(pointer);
    public static void Read(this IntPtr pointer, NetworkBuffer buffer) => Marshal.Copy(Marshal.PtrToStructure<SteamNetworkingMessage_t>(pointer).m_pData, buffer.Payload, 0, buffer.Payload.Length);
    public static void Release(this IntPtr pointer) => NativeMethods.SteamAPI_SteamNetworkingMessage_t_Release(pointer);
}

@fghl
Copy link
Author

fghl commented Jun 4, 2021

Thanks, that's really helpful. For now I've paused my Steamworks.NET work and am porting to Facepunch.Steamworks until there's an official fix here. They may also have issues, in which case I'll come back and try this.

@fghl
Copy link
Author

fghl commented Jun 4, 2021

Their interface is even more broken. Trying this.

@fghl
Copy link
Author

fghl commented Jun 6, 2021

@AG4W This worked as advertised, thanks for the help!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants