Skip to content

Commit

Permalink
Refs #10311. Add additional debugging capabilities to ReferenceCounter.
Browse files Browse the repository at this point in the history
The original ReferenceCounter class would print out the QObject's objectName().
I didn't implement this when I rewrote ReferenceCounter as a general purpose
reference counter. But having a name for an object does aid in debugging, this
reintroduces that idea but with a QString passed into the constructor. By
default this is not enabled, but if you need to debug reference counting
enabling it is just a matter of uncommenting a "//#define EXTRA_DEBUG" in
the header and recompiling.
  • Loading branch information
daniel-kristjansson committed Jun 1, 2012
1 parent 671017f commit f0bfe0f
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 19 deletions.
9 changes: 6 additions & 3 deletions mythtv/libs/libmythbase/mythcommandlineparser.cpp
Expand Up @@ -143,7 +143,8 @@ const char* NamedOptType(int type)
*/
CommandLineArg::CommandLineArg(QString name, QVariant::Type type,
QVariant def, QString help, QString longhelp) :
ReferenceCounter(), m_given(false), m_converted(false), m_name(name),
ReferenceCounter(QString("CommandLineArg:%1").arg(name)),
m_given(false), m_converted(false), m_name(name),
m_group(""), m_deprecated(""), m_removed(""), m_removedversion(""),
m_type(type), m_default(def), m_help(help), m_longhelp(longhelp)
{
Expand All @@ -159,7 +160,8 @@ CommandLineArg::CommandLineArg(QString name, QVariant::Type type,
* supplied directly on the command line.
*/
CommandLineArg::CommandLineArg(QString name, QVariant::Type type, QVariant def)
: ReferenceCounter(), m_given(false), m_converted(false), m_name(name),
: ReferenceCounter(QString("CommandLineArg:%1").arg(name)),
m_given(false), m_converted(false), m_name(name),
m_group(""), m_deprecated(""), m_removed(""), m_removedversion(""),
m_type(type), m_default(def)
{
Expand All @@ -176,7 +178,8 @@ CommandLineArg::CommandLineArg(QString name, QVariant::Type type, QVariant def)
* name prior to parsing inputs.
*/
CommandLineArg::CommandLineArg(QString name) :
ReferenceCounter(), m_given(false), m_converted(false), m_name(name),
ReferenceCounter(QString("CommandLineArg:%1").arg(name)),
m_given(false), m_converted(false), m_name(name),
m_deprecated(""), m_removed(""), m_removedversion(""),
m_type(QVariant::Invalid)
{
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythversion.h
Expand Up @@ -12,7 +12,7 @@
/// Update this whenever the plug-in API changes.
/// Including changes in the libmythbase, libmyth, libmythtv, libmythav* and
/// libmythui class methods used by plug-ins.
#define MYTH_BINARY_VERSION "0.26.20120528-1"
#define MYTH_BINARY_VERSION "0.26.20120531-1"

/** \brief Increment this whenever the MythTV network protocol changes.
*
Expand Down
22 changes: 19 additions & 3 deletions mythtv/libs/libmythbase/referencecounter.cpp
@@ -1,11 +1,17 @@
// -*- Mode: c++ -*-

#include <QString>

#include "referencecounter.h"
#include "mythlogging.h"

ReferenceCounter::ReferenceCounter(void) : m_referenceCount(1)
ReferenceCounter::ReferenceCounter(const QString &debugName) :
#ifdef EXTRA_DEBUG
m_debugName(debugName),
#endif
m_referenceCount(1)
{
(void) debugName;
}

ReferenceCounter::~ReferenceCounter(void)
Expand All @@ -19,18 +25,28 @@ ReferenceCounter::~ReferenceCounter(void)

void ReferenceCounter::IncrRef(void)
{
int val = m_referenceCount.fetchAndAddRelease(1);
int val = m_referenceCount.fetchAndAddRelease(1) + 1;

#ifdef EXTRA_DEBUG
LOG(VB_REFCOUNT, LOG_DEBUG, QString("%1(0x%2)::IncrRef() -> %3")
.arg(m_debugName).arg(reinterpret_cast<intptr_t>(this),0,16).arg(val));
#else
LOG(VB_REFCOUNT, LOG_DEBUG, QString("(0x%2)::IncrRef() -> %3")
.arg(reinterpret_cast<intptr_t>(this),0,16).arg(val));
#endif
}

bool ReferenceCounter::DecrRef(void)
{
int val = m_referenceCount.fetchAndAddRelaxed(-1);
int val = m_referenceCount.fetchAndAddRelaxed(-1) - 1;

#ifdef EXTRA_DEBUG
LOG(VB_REFCOUNT, LOG_DEBUG, QString("%1(0x%2)::DecrRef() -> %3")
.arg(m_debugName).arg(reinterpret_cast<intptr_t>(this),0,16).arg(val));
#else
LOG(VB_REFCOUNT, LOG_DEBUG, QString("(0x%2)::DecrRef() -> %3")
.arg(reinterpret_cast<intptr_t>(this),0,16).arg(val));

This comment has been minimized.

Copy link
@jyavenard

jyavenard Jun 1, 2012

Member

This is wrong..
It should be :
LOG(VB_REFCOUNT, LOG_DEBUG, QString("(0x%1)::DecrRef() -> %2")
.arg(reinterpret_cast<intptr_t>(this),0,16).arg(val));

and same for all the other line of this type

#endif

if (0 == val)
{
Expand Down
15 changes: 14 additions & 1 deletion mythtv/libs/libmythbase/referencecounter.h
Expand Up @@ -3,7 +3,17 @@
#ifndef MYTHREFCOUNT_H_
#define MYTHREFCOUNT_H_

// Uncomment this line for more useful reference counting debugging
//#define EXTRA_DEBUG

#include <QAtomicInt>

#ifdef EXTRA_DEBUG
#include <QString>
#else
class QString;
#endif

#include "mythbaseexp.h"

/** \brief General purpose reference counter.
Expand All @@ -15,7 +25,7 @@ class MBASE_PUBLIC ReferenceCounter
{
public:
/// Creates reference counter with an initial value of 1.
ReferenceCounter(void);
ReferenceCounter(const QString &debugName);

/// Increments reference count.
void IncrRef(void);
Expand All @@ -30,6 +40,9 @@ class MBASE_PUBLIC ReferenceCounter
virtual ~ReferenceCounter(void);

private:
#ifdef EXTRA_DEBUG
const QString m_debugName;
#endif
QAtomicInt m_referenceCount;
};

Expand Down
Expand Up @@ -17,12 +17,13 @@
#include "programinfo.h"

DeleteHandler::DeleteHandler(void) :
ReferenceCounter(), m_fd(-1), m_size(0)
ReferenceCounter("DeleteHandler"), m_fd(-1), m_size(0)
{
}

DeleteHandler::DeleteHandler(QString filename) :
ReferenceCounter(), m_path(filename), m_fd(-1), m_size(0)
ReferenceCounter(QString("DeleteHandler:%1").arg(filename)),
m_path(filename), m_fd(-1), m_size(0)
{
}

Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythprotoserver/sockethandler.cpp
Expand Up @@ -8,7 +8,8 @@ using namespace std;

SocketHandler::SocketHandler(MythSocket *sock, MythSocketManager *parent,
QString hostname) :
ReferenceCounter(), m_blockShutdown(false), m_standardEvents(false),
ReferenceCounter("SocketHandler"),
m_blockShutdown(false), m_standardEvents(false),
m_systemEvents(false), m_socket(sock), m_parent(parent),
m_hostname(hostname)
{
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythupnp/ssdpcache.cpp
Expand Up @@ -27,7 +27,7 @@ int SSDPCacheEntries::g_nAllocated = 0; // Debugging only
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

SSDPCacheEntries::SSDPCacheEntries()
SSDPCacheEntries::SSDPCacheEntries() : ReferenceCounter("SSDPCacheEntries")
{
g_nAllocated++; // Should be atomic increment
}
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythupnp/taskqueue.cpp
Expand Up @@ -38,7 +38,7 @@ long Task::m_nTaskCount = 0;
//
/////////////////////////////////////////////////////////////////////////////

Task::Task()
Task::Task(const QString &debugName) : ReferenceCounter(debugName)
{
m_nTaskId = m_nTaskCount++;
}
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythupnp/taskqueue.h
Expand Up @@ -65,7 +65,7 @@ class Task : public ReferenceCounter

public:

Task();
Task(const QString &debugName);

long Id() const { return( m_nTaskId ); }

Expand Down
4 changes: 3 additions & 1 deletion mythtv/libs/libmythupnp/upnpdevice.h
Expand Up @@ -236,7 +236,9 @@ class UPNP_PUBLIC DeviceLocation : public ReferenceCounter
DeviceLocation( const QString &sURI,
const QString &sUSN,
const QString &sLocation,
TaskTime ttExpires ) : m_pDeviceDesc( NULL ),
TaskTime ttExpires ) : ReferenceCounter(
"DeviceLocation" ),
m_pDeviceDesc( NULL ),
m_sURI ( sURI ),
m_sUSN ( sUSN ),
m_sLocation ( sLocation ),
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythupnp/upnptaskcache.h
Expand Up @@ -39,7 +39,7 @@ class SSDPCacheTask : public Task

public:

SSDPCacheTask()
SSDPCacheTask() : Task("SSDPCacheTask")
{
m_nExecuteCount = 0;
m_nInterval = 1000 *
Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythupnp/upnptaskevent.cpp
Expand Up @@ -27,7 +27,8 @@

UPnpEventTask::UPnpEventTask( QHostAddress peerAddress,
int nPeerPort,
QByteArray *pPayload )
QByteArray *pPayload ) :
Task("UPnpEventTask")
{
m_PeerAddress = peerAddress;
m_nPeerPort = nPeerPort;
Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythupnp/upnptasknotify.cpp
Expand Up @@ -37,7 +37,8 @@
//
/////////////////////////////////////////////////////////////////////////////

UPnpNotifyTask::UPnpNotifyTask( int nServicePort )
UPnpNotifyTask::UPnpNotifyTask( int nServicePort ) :
Task("UPnpNotifyTask")
{
m_nServicePort = nServicePort;
m_eNTS = NTS_alive;
Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythupnp/upnptasksearch.cpp
Expand Up @@ -38,7 +38,8 @@ UPnpSearchTask::UPnpSearchTask( int nServicePort,
QHostAddress peerAddress,
int nPeerPort,
QString sST,
QString sUDN )
QString sUDN ) :
Task("UPnpSearchTask")
{
m_PeerAddress = peerAddress;
m_nPeerPort = nPeerPort;
Expand Down

0 comments on commit f0bfe0f

Please sign in to comment.