I am trying to clean up someone else’s code and I see a lot of cases where a class contains a pthread_mutex_t member variable. In a number of cases, pthread_mutex_init() is never called, nor is lock/unlock. When that object goes out of scope, what, if anything, needs to be done to properly clean up after it? Can I try to unlock it (if it hasn’t been init’d, what happens)? Do I need to destroy it?
The analysis tool is saying that the resource may be lost. Has anything been lost?
If you’re doing a cleanup and you find a mutex which is never used, you should consider removing it.
If the mutex is used, you should call
pthread_mutex_destroy(). Class destructor is a good place to do it, especially ifpthread_mutex_init()is called in the constructor.Note that the mutex must be unlocked by the time it is destroyed or undefined behavior will result. This should be ensured by the way mutex is used however.