Permalink
Browse files

Fix some issues found by cppcheck

Serves me right for not running cppcheck on the branch pre-merge.  There were
some memory leakage opportunities, some uninitialized members and even some
pass by reference recommendations.  Fun.
  • Loading branch information...
1 parent d9ac417 commit af3f93993da9aed600f010edac3043e65533bb7b @Beirdo Beirdo committed Jun 14, 2012
View
30 mythtv/libs/libmythbase/logging.cpp
@@ -142,8 +142,8 @@ LoggingItem::LoggingItem()
LoggingItem::LoggingItem(const char *_file, const char *_function,
int _line, LogLevel_t _level, LoggingType _type) :
m_threadId((uint64_t)(QThread::currentThreadId())),
- m_line(_line), m_type(_type), m_level(_level), m_file(_file),
- m_function(_function), m_threadName(NULL)
+ m_line(_line), m_type(_type), m_level(_level),
+ m_file(strdup(_file)), m_function(strdup(_function)), m_threadName(NULL)
{
loggingGetTimeStamp(&m_epoch, &m_usec);
@@ -153,6 +153,26 @@ LoggingItem::LoggingItem(const char *_file, const char *_function,
refcount.ref();
}
+LoggingItem::~LoggingItem()
+{
+ if (m_file)
+ free((void *)m_file);
+
+ if (m_function)
+ free((void *)m_function);
+
+ if (m_threadName)
+ free(m_threadName);
+
+ if (m_appName)
+ free((void *)m_appName);
+
+ if (m_table)
+ free((void *)m_table);
+
+ if (m_logFile)
+ free((void *)m_logFile);
+}
QByteArray LoggingItem::toByteArray(void)
{
@@ -176,8 +196,8 @@ char *LoggingItem::getThreadName(void)
QMutexLocker locker(&logThreadMutex);
char *name = logThreadHash.value(m_threadId, (char *)unknown);
- m_threadName = name;
- return name;
+ m_threadName = strdup(name);
+ return m_threadName;
}
/// \brief Get the thread ID of the thread that produced the LoggingItem
@@ -664,8 +684,6 @@ void LoggingItem::deleteItem(void)
{
if (!refcount.deref())
{
- if (m_threadName)
- free(m_threadName);
item_count.deref();
this->deleteLater();
}
View
15 mythtv/libs/libmythbase/logging.h
@@ -106,19 +106,19 @@ class LoggingItem: public QObject
void setLevel(const int val) { m_level = (LogLevel_t)val; };
void setFacility(const int val) { m_facility = val; };
void setEpoch(const qlonglong val) { m_epoch = val; };
- void setFile(const QString val)
+ void setFile(const QString &val)
{ m_file = strdup(val.toLocal8Bit().constData()); };
- void setFunction(const QString val)
+ void setFunction(const QString &val)
{ m_function = strdup(val.toLocal8Bit().constData()); };
- void setThreadName(const QString val)
+ void setThreadName(const QString &val)
{ m_threadName = strdup(val.toLocal8Bit().constData()); };
- void setAppName(const QString val)
+ void setAppName(const QString &val)
{ m_appName = strdup(val.toLocal8Bit().constData()); };
- void setTable(const QString val)
+ void setTable(const QString &val)
{ m_table = strdup(val.toLocal8Bit().constData()); };
- void setLogFile(const QString val)
+ void setLogFile(const QString &val)
{ m_logFile = strdup(val.toLocal8Bit().constData()); };
- void setMessage(const QString val)
+ void setMessage(const QString &val)
{
strncpy(m_message, val.toLocal8Bit().constData(), LOGLINE_MAX);
m_message[LOGLINE_MAX] = '\0';
@@ -154,6 +154,7 @@ class LoggingItem: public QObject
LoggingItem();
LoggingItem(const char *_file, const char *_function,
int _line, LogLevel_t _level, LoggingType _type);
+ ~LoggingItem();
};
/// \brief The logging thread that consumes the logging queue and dispatches
View
11 mythtv/libs/libmythbase/loggingserver.cpp
@@ -135,7 +135,7 @@ LoggerBase::~LoggerBase()
/// \brief FileLogger constructor
/// \param filename Filename of the logfile.
FileLogger::FileLogger(const char *filename) :
- LoggerBase(filename), m_opened(false), m_fd(-1)
+ LoggerBase(filename), m_opened(false), m_fd(-1), m_zmqSock(NULL)
{
m_fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, 0664);
m_opened = (m_fd != -1);
@@ -247,7 +247,8 @@ void FileLogger::setupZMQSocket(void)
#ifndef _WIN32
/// \brief SyslogLogger constructor
/// \param facility Syslog facility to use in logging
-SyslogLogger::SyslogLogger() : LoggerBase(NULL), m_opened(false)
+SyslogLogger::SyslogLogger() :
+ LoggerBase(NULL), m_opened(false), m_zmqSock(NULL)
{
openlog(NULL, LOG_NDELAY, 0 );
m_opened = true;
@@ -308,7 +309,8 @@ void SyslogLogger::setupZMQSocket(void)
// Windows doesn't have syslog support
-SyslogLogger::SyslogLogger() : LoggerBase(NULL), m_opened(false)
+SyslogLogger::SyslogLogger() :
+ LoggerBase(NULL), m_opened(false), m_zmqSock(NULL)
{
}
@@ -333,7 +335,8 @@ const int DatabaseLogger::kMinDisabledTime = 1000;
/// \brief DatabaseLogger constructor
/// \param table C-string of the database table to log to
DatabaseLogger::DatabaseLogger(const char *table) :
- LoggerBase(table), m_opened(false), m_loggingTableExists(false)
+ LoggerBase(table), m_opened(false), m_loggingTableExists(false),
+ m_zmqSock(NULL)
{
m_query = QString(
"INSERT INTO %1 "

0 comments on commit af3f939

Please sign in to comment.