I am making a video game and I have a main thread which takes care of the graphics, and a separate worker thread which takes care of the networking (synchronous UDP sockets).
The problem is that when I close the game, I get an “Access violation” error, and the debugger points me to one of several lines of code–each of which references an object which I believe already got destroyed in the other thread.
Since I am using asynchronous threads my instinct is that the main thread almost always gets killed first while the recv() call is blocking, and then finally when recv() returns, all the pointer it now holds are bogus and visual studio starts throwing errors.
BTW I’m using Boost threads, Visual Studio 2008 and C++
Before cleaning up shared resources, the main thread should wait for the networking thread to complete. In Boost, the
boost::thread::join()function should do the trick.Be sure to deal with
boost::thread_interruptedas well!(N.B. This is sort of a generic answer; I have no experience with Boost threading in particular.)