For efficiency reasons, SDL does not provide built-in synchronization/mutual exclusion. Adding a mutex around your map should fix the issue.
Random EXC_BAD_ACCESS due to something I must have overlooked
-
05-12-2021 - |
Question
I'm back to C++ after a long while, and I wanted to do it with style so I dived face-first into libCinder and started doing stuff I had rarely done before (and even then I used to prefer Qt for a number of reasons). What can go wrong with this plan?
The thing is that very little does, especially when I run my code from inside Xcode using the Debug profile and with several safety belts strapped on (aka debugger), but then I have to go into the wild and nasty stuff happens: users get too excited, something is not tight enough, and eventually my application miserably crashes.
I managed to narrow down the culprits to the PilotStudyApp::draw() function (see the code here https://github.com/Morpheu5/PilotStudy), most likely around the last for loop, the one that goes through the _activePoints, but I can't be sure as you can see from the bt
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libstdc++.6.dylib 0x914635e4 std::_Rb_tree_increment(std::_Rb_tree_node_base*) + 15
1 org.cinder.PilotStudy 0x000916b3 PilotStudyApp::draw() + 419
2 org.cinder.PilotStudy 0x000c34ad -[CinderView drawRect:] + 125
that you can see entirely here: http://pastebin.com/izZcFy8p
I have a strong feeling that something's up with my C++-fu (or lack thereof) so if you guys can spot something I have overlooked, I'll be extremely grateful (since I don't want to go for Processing, it'd feel like going back to kindergarten :)
EDIT: It just occurred to me that STL may not be thread-safe so synchronising, or making local copies, should resolve perhaps?
Solution
OTHER TIPS
As @alexp pointed out, concurrent reading from and writing to STL containers from multiple threads is not supported.
You can make your life easier, however, if you follow Anthony Williams' excellent article: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
For your convenience, I have written a few concurrent container classes based on his article, that you can find here: https://github.com/paulhoux/Cinder-Samples/tree/master/FlickrImageViewer/include/ph