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

Calling SteamNetworkingMessage_t.Release() causes a crash #431

Closed
simonpriest-fl opened this issue Jul 1, 2021 · 1 comment
Closed

Calling SteamNetworkingMessage_t.Release() causes a crash #431

simonpriest-fl opened this issue Jul 1, 2021 · 1 comment

Comments

@simonpriest-fl
Copy link

There's a Release method defined for SteamNetworkingMessage_t which will cause a crash if called.

The C# method passes the m_pfnRelease pointer as the parameter to the release function:

        public void Release() {
            NativeMethods.SteamAPI_SteamNetworkingMessage_t_Release(m_pfnRelease);
        }

But the implementation of this function (at least, the one visible in the open-source version of the Steam library) seems to be expecting a pointer to the network message itself:

STEAMNETWORKINGSOCKETS_INTERFACE void SteamAPI_SteamNetworkingMessage_t_Release( SteamNetworkingMessage_t* self )
{
	self->Release(  );
}

And the code in steamnetworkingtypes.h defines m_pfnRelease as a function pointer:

	/// Function to used to decrement the internal reference count and, if
	/// it's zero, release the message.  You should not set this function pointer,
	/// or need to access this directly!  Use the Release() function instead!
	void (*m_pfnRelease)( SteamNetworkingMessage_t *pMsg );

And the C++ version of Release is as follows, passing this into a call to m_pfnRelease:

inline void SteamNetworkingMessage_t::Release() { (*m_pfnRelease)( this ); }

The safe way to release a message appears to be to call SteamAPI_SteamNetworkingMessage_t_Release and pass the original IntPtr obtained from ReceiveMessagesOnPollGroup (and friends), or maybe to marshal the message's m_pfnRelease into a delegate and call it like the C++ version.

It seems like having Release as a method on the C# struct will never work, because the struct doesn't contain a pointer to itself that we can pass to SteamAPI_SteamNetworkingMessage_t_Release, and having unmarshalled the struct in the first place means an instance won't have the same address as the one Steam allocated.

@rlabrecque
Copy link
Owner

rlabrecque commented Jul 18, 2021

I believe this one was fixed by this change?

#425

# 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

2 participants