- Home /
Segfault in native plugin code, only with Unity
I'm running a native plugin that starts a Live555 media server on a separate thread. The server waits for a client to connect, then starts transmitting (empty, for now) data to the client. When I run this code by loading the .bundle in a C++ program, it runs and transmits fine, however when I run it by calling from Unity, it segfaults 4/5 times in random places deep within the server code. Only every once in a while (1/5 times) does it connect and transmit the data correctly.
I have also tried without starting a thread which causes the same issue in addition to completely freezing Unity because the server is in an infinite loop.
What could be causing these random memory issues with native plugins? Seems like a race condition but I have no idea why. As an alternative, is there possibly another way I could start this server from Unity but keep only the data I want transmitted in shared memory, keeping the Unity editor/application and Live555 server out of shared memory?
In case there is another way to call it, I've copied my C# code below-- I call the thread like this:
void Awake()
{
ServerThread server = new ServerThread();
Thread st = new Thread(new ThreadStart(server.startServer));
st.Start();
}
The thread code:
public class ServerThread : MonoBehaviour
{
[DllImport ("RenderingPlugin")]
private static extern int startStream();
// This method that will be called when the thread is started
public void startServer()
{
Debug.Log ("Thread started.");
startStream (); // Starts the server in native code
}
};
I "solved" the problem by working around it. I still have no idea what was causing the segfault, maybe it was too much thread unsafe code operating in the same shared memory. So ins$$anonymous$$d of starting a new Live 555 server thread from Unity, I started a separate Live 555 server process. I used inter-process communication via boost libraries to establish a spot in memory that can be shared by the server and Unity.