I have found a number of 'small' memory leaks in XrdClient that are
coming from 'pthread_mutexattr_init'. The problem is once the mutex
attributes has been used, 'pthread_mutexattr_destroy' is not being
called. I'd suggest wrapping the two in a small class
class XrdClientMutexAttr {
pthread_mutexattr_t fAttr;
bool fOK;
public:
enum Type {RECURSIVE, NONRECURSIVE};
XrdClientMutexAttr(Type iType = NONRECURSIVE) : fOK(true) {
int rc = pthread_mutexattr_init(&fAttr);
if( rc ) {
fOK = false;
}
if(RECURSIVE == iType ) {
rc = pthread_mutexattr_settype(&fAttr, PTHREAD_MUTEX_RECURSIVE);
}
if( rc ) {
fOK = false;
pthread_mutexattr_destroy(&fAttr);
}
}
~XrdClientMutexAttr() {
if (fOK) { pthread_mutexattr_destroy(&fAttr); }
}
pthread_mutexattr_t* get() { return fOK ? &fAttr :
((pthread_mutexattr_t*)0) ; }
bool initialize(pthread_mutex_t* iMutex) {
if( fOK && (0 == pthread_mutex_init(iMutex, get() ) ) ) {
return true;
}
return false;
}
};
It would be nicer to user exceptions rather than 'fOK'.
This would allow the code to be reduced to
// Initialization of lock mutex
XrdClientMutexAttr attr(XrdClientMutexAttr::RECURSIVE);
if( attr.initialize( &fMutex ) ) {
Error("InputBuffer",
"Can't create mutex: out of system resources.");
abort();
}
Chris
|