Heya,
I’m writing a socket server in C# that uses 1 thread and select statements to send and receive data.
I’ve put quite some effort into making it thread safe with locked queues for messages that go into Unity code and so on.
And the weird thing is that I’m experiencing no uncaught exceptions while testing the code externally in Visual Studio… but sporadic crashes while running it in the Unity environment. These crashes mainly happen when stopping the editor from Play mode. What could be causing this? I believe I’m stopping the threads properly with volatile bools and Join() and so on, in OnApplicationQuit().
Any help appreciated.
I’m sure there was a thread on that already.
In Unity 2.6 networking sockets that are not closed correctly basically create a lot of problems (even unity’s own networking which basically renders you inable to edit more than a single value on a variable before the go is unselected again)
You could check if using CancelQuit and waiting for all threads to finish correctly followed by calling Quit afterwards helps.
if they aren’t down by the end of OnApplicationQuit though, they will leave an open socket with assigned but no invalid delegate / thread and that will mess the whole application (the game runs within the same mono environment as the editor, so messing the game environment will trash the editor environment kind of)
The clean way would be shutting down the network first, then shutting down the rest of the application.
Thanks.
However this appears to have been caused by Debug.Log() calls from other Threads. Some of them didn’t even get to the log window…
So I assume that Debug.Log() isn’t threadsafe either. Which makes sense maybe.
It’s funny though… if you look at this thing:
http://www.unifycommunity.com/wiki/index.php?title=Server
it uses Debug.Log() A LOT from an external thread, which doesn’t seem like a great idea.
Is there any example about queuing ? iì’m having same problems using a c# socket to receive (and move) camera over the net