Do profile freeing under lock - avoid double free issues
(cherry picked from commit b7928cf62e4f13f4709114a972018016e58d91e9)