I figured out the issue but I seek understanding.
My issue is that I have an object that creates a thread to run one of its function.
void WatchHook::start() {
if (!is_running) {
is_running = true;
watch_thread = std::thread(&WatchHook::_run, this);
}
}
void WatchHook::_run() {
blah code.....
}
in start() the instance of WatchHook is correct with everything being as it should be. However, in run, the instance of WatchHook gets corrupted somehow with variables randomly reassigning to everything, looks like a memory issue.
I tried basically everything I could think of to fix this. I verified exactly where the breakdown was happening, between the start call that creates the thread, and the thread instantiating and running. I tried mutex locking everything the thread touched so see if it was maybe interference between threads I didn’t catch. I tried different numbers of threads, more caused more chaos in the variables with more being corrupt.
What finally fixed the issue was not calling WatchHook.start() from this constructor
Player::Player(Point position, cv::Mat template_image, HWND hwnd)
: position(position),
watcher(position, template_image, hwnd)
{
//watcher.start() <- this was the naughty boy
}
and instead calling it from main
for (int i = 0; i < 4; i++) {
players.at(i).watcher.start();
}
Can anyone tell me what is happening here? Im pretty sure its not race condition between the object instantiating and the thread creation as the data is right in the start function. I also mutexed basically everything in testing so im pretty sure its not data collision either.
Mica is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3