Skip to content

Commit

Permalink
Debug|libdeng2: Investigating odd log behavior (invalid freed pointers)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Dec 6, 2012
1 parent c3341be commit 6560e58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/core/library.cpp
Expand Up @@ -65,7 +65,7 @@ Library::Library(NativePath const &nativePath) : d(0)
{
d = new Instance;

LOG_AS("Library::Library");
LOG_AS("Library");
LOG_TRACE("Loading ") << nativePath.pretty();

#ifndef DENG2_USE_DLOPEN
Expand Down
32 changes: 18 additions & 14 deletions doomsday/libdeng2/src/core/log.cpp
Expand Up @@ -28,6 +28,7 @@
#include <QTextStream>
#include <QThread>
#include <QStringList>
#include <QDebug>

namespace de {

Expand All @@ -49,19 +50,18 @@ class Logs : public QMap<QThread *, Log *>, public Lockable
public:
Logs() {}
~Logs() {
lock();
DENG2_GUARD(this);
// The logs are owned by the logs table.
foreach(Log *log, values()) {
delete log;
}
unlock();
}
};

} // namespace internal

/// The logs table contains the log of each thread that uses logging.
static internal::Logs logs;
static std::auto_ptr<internal::Logs> logsPtr;

LogEntry::LogEntry() : _level(TRACE), _sectionDepth(0), _disabled(true)
{}
Expand Down Expand Up @@ -355,36 +355,40 @@ LogEntry &Log::enter(LogEntry::Level level, String const &format, LogEntry::Args

Log &Log::threadLog()
{
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
internal::Logs& logs = *logsPtr;
DENG2_GUARD(logs);

// Each thread has its own log.
QThread *thread = QThread::currentThread();
Log *theLog = 0;
logs.lock();
internal::Logs::iterator found = logs.find(thread);
if(found == logs.end())
internal::Logs::const_iterator found = logs.constFind(thread);
if(found == logs.constEnd())
{
// Create a new log.
theLog = new Log();
logs[thread] = theLog;
Log* theLog = new Log;
qDebug() << "Log" << theLog << "created for thread" << thread;
logs.insert(thread, theLog);
return *theLog;
}
else
{
theLog = found.value();
return *found.value();
}
logs.unlock();
return *theLog;
}

void Log::disposeThreadLog()
{
if(!logsPtr.get()) logsPtr.reset(new internal::Logs);
internal::Logs& logs = *logsPtr;
DENG2_GUARD(logs);

QThread *thread = QThread::currentThread();
logs.lock();
internal::Logs::iterator found = logs.find(thread);
if(found != logs.end())
{
delete found.value();
logs.remove(found.key());
}
logs.unlock();
}

LogEntryStager::LogEntryStager(LogEntry::Level level, String const &format) : _level(level)
Expand Down

0 comments on commit 6560e58

Please sign in to comment.