Changed to pass the shared_ptr by reference.
Problem: The UPS callback function crashed on Linux when it is called, which is reproducible.
Hunch: printUPS pass std::shared_ptr by value, which is create a new instance of std::shared_ptr. Later it is passed by reference into the callback Lambda function. After printUPS scope ends, the reference becomes invalid.
Resolution:
-
Pass the std::shared_ptr by reference, so that the Lambda function get a reference of the original shared_ptr in the parent scope.
-
Pass the capture by value to the Lambda function, so that it owns another shared_ptr of the UPSCounter.
-
is applied. So long as the parent scope's shared_ptr will be destroyed after the UPSCounter, the reference in the callback will retain valid.