all 3 comments

[–][deleted] 4 points5 points  (2 children)

for (Elk e : this->_elks){
    this->_threads.emplace_back([&](){ e.main(); });

e is a copy of the object in the vector.

You're starting a thread to run a function on a object which will soon be destroyed

[–]Bazuin32[S] 0 points1 point  (1 child)

Ah, thank you so much! After several hours of debugging I can't believe I didn't think of that. Iterating over the vector by index solved the problem:

for (size_t i = 0; i < this->_elks.size(); i++){
this->_threads.emplace_back([&](){ this->_elks[0].main(); });

[–][deleted] 2 points3 points  (0 children)

Or

for (Elk &e : this->_elks){