Overcoming “Process finished with exit code 139 (interrupted by signal 11: SIGSEGV”

While working on my project Cloudbank and implementing some C++ multithreading into it, I encountered the following error message:

"Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)"

after some debugging I pinpointed the code block that was causing the error:

auto dark_contrast_frame_objects  = std::async(std::launch::async,std::bind(&SeperateObjects::BoundBox, &frame_objects,binary_image_dark_contrast, darkComtrastImage, auto dark_contrast_frame_objects  = std::async(std::launch::async,std::bind(&SeperateObjects::BoundBox, &frame_objects,binary_image_dark_contrast, darkComtrastImage, std::ref(dark_x_coordinate), std::ref(dark_y_coordinate), std::ref(bound_rect_for_dark_contrast_frame), recordDarkContrastIdentifiedObjects));
auto light_contrast_frame_objects = std::async(std::launch::async,std::bind(&SeperateObjects::BoundBox, &frame_objects,binary_image_light_contrast, lightContrastImage, std::ref(light_x_coordinate), std::ref(light_y_coordinate), std::ref(bound_rect_for_light_contrast_frame), recordLightContrastIdentifiedObjects));

//Append/combine boundbox vectors so that all objects can be put into the python dictionary 
bound_rect_for_dark_contrast_frame.insert(bound_rect_for_dark_contrast_frame.end(), bound_rect_for_light_contrast_frame.begin(), bound_rect_for_light_contrast_frame.end());

have you noticed the problem?

I encountered a race condition. I used “std::async()” to assign a value to my “dark_contrast_frame_objects” and “light_contrast_frame_objects” objects and went right away to use them without waiting for “std::async()” to complete for each of them so I was trying to access something that wasn’t there.

I fixed this by adding “.wait()” to the objects before the “bound_rect_for_dark_contrast_frame.insert()” line that uses the objects as arguments.

auto dark_contrast_frame_objects  = std::async(std::launch::async,std::bind(&SeperateObjects::BoundBox, &frame_objects,binary_image_dark_contrast, darkComtrastImage, std::ref(dark_x_coordinate), std::ref(dark_y_coordinate), std::ref(bound_rect_for_dark_contrast_frame), recordDarkContrastIdentifiedObjects));
auto light_contrast_frame_objects = std::async(std::launch::async,std::bind(&SeperateObjects::BoundBox, &frame_objects,binary_image_light_contrast, lightContrastImage, std::ref(light_x_coordinate), std::ref(light_y_coordinate), std::ref(bound_rect_for_light_contrast_frame), recordLightContrastIdentifiedObjects));
dark_contrast_frame_objects.wait();
light_contrast_frame_objects.wait(); 
//Append/combine boundbox vectors so that all objects can be put into the python dictionary 
bound_rect_for_dark_contrast_frame.insert(bound_rect_for_dark_contrast_frame.end(), bound_rect_for_light_contrast_frame.begin(), bound_rect_for_light_contrast_frame.end());

this ensures that std::async() completes for each of the objects before the program makes use of them.

Passing Reference agrguments into  std::async()

When passing in aguments into a function that will be paralized using std::aync() for example:

std::async(std::launch::async,std::bind(&<CLASS>::<CLASS METHOD>, &VARIBLE_NAME_FOR_CLASS ,<CLASS_METHOD_ARGUMENT>,<CLASS_METHOD_ARGUMENT>, <CLASS_METHOD_REFERENCE_ARGUMENT>)

And you want to pass in a argument into the function via a refernce( which is the meaning of <CLASS_METHOD_REFERENCE_ARGUMENT>) then you must use the “std::ref()” methon to do so for example:

std::async(std::launch::async,std::bind(&<CLASS>::<CLASS METHOD>, &VARIBLE_NAME_FOR_CLASS ,<CLASS_METHOD_ARGUMENT>,<CLASS_METHOD_ARGUMENT>, std::ref(<CLASS_METHOD_REFERENCE_ARGUMENT>))

this is because arguments are by default passed by value when multithreading in c++. You have to use “std::ref ()”to ensure that a value is passed in by refernce.