Skip to content

Commit

Permalink
Fix some memory leaks (#3779)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-w authored Aug 28, 2017
1 parent efd0d34 commit 24d320d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
13 changes: 8 additions & 5 deletions include/PluginFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef PLUGINFACTORY_H
#define PLUGINFACTORY_H

#include <memory>

#include <QtCore/QFileInfo>
#include <QtCore/QList>

Expand All @@ -39,14 +41,15 @@ class EXPORT PluginFactory
struct PluginInfo
{
PluginInfo() : library(nullptr), descriptor(nullptr) {}

const QString name() const;
QFileInfo file;
QLibrary* library;
std::shared_ptr<QLibrary> library;
Plugin::Descriptor* descriptor;

bool isNull() const {return library == 0;}
bool isNull() const {return ! library;}
};
typedef QList<PluginInfo*> PluginInfoList;
typedef QList<PluginInfo> PluginInfoList;
typedef QMultiMap<Plugin::PluginTypes, Plugin::Descriptor*> DescriptorMap;

PluginFactory();
Expand Down Expand Up @@ -80,11 +83,11 @@ public slots:
private:
DescriptorMap m_descriptors;
PluginInfoList m_pluginInfos;
QMap<QString, PluginInfo*> m_pluginByExt;
QMap<QString, PluginInfo> m_pluginByExt;

QHash<QString, QString> m_errors;

static PluginFactory* s_instance;
static std::unique_ptr<PluginFactory> s_instance;
};

#define pluginFactory PluginFactory::instance()
Expand Down
4 changes: 2 additions & 2 deletions include/Song.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EXPORT Song : public TrackContainer
void clearErrors();
void collectError( const QString error );
bool hasErrors();
QString* errorSummary();
QString errorSummary();

class PlayPos : public MidiTime
{
Expand Down Expand Up @@ -359,7 +359,7 @@ private slots:

bool m_loadingProject;

QList<QString> * m_errors;
QStringList m_errors;

PlayModes m_playMode;
PlayPos m_playPos[Mode_Count];
Expand Down
37 changes: 15 additions & 22 deletions src/core/PluginFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ qint64 qHash(const QFileInfo& fi)
return qHash(fi.absoluteFilePath());
}

PluginFactory* PluginFactory::s_instance = nullptr;
std::unique_ptr<PluginFactory> PluginFactory::s_instance;

PluginFactory::PluginFactory()
{
Expand Down Expand Up @@ -87,9 +87,9 @@ PluginFactory::~PluginFactory()
PluginFactory* PluginFactory::instance()
{
if (s_instance == nullptr)
s_instance = new PluginFactory();
s_instance.reset(new PluginFactory());

return s_instance;
return s_instance.get();
}

const Plugin::DescriptorList PluginFactory::descriptors() const
Expand All @@ -109,16 +109,15 @@ const PluginFactory::PluginInfoList& PluginFactory::pluginInfos() const

const PluginFactory::PluginInfo PluginFactory::pluginSupportingExtension(const QString& ext)
{
PluginInfo* info = m_pluginByExt.value(ext, nullptr);
return info == nullptr ? PluginInfo() : *info;
return m_pluginByExt.value(ext, PluginInfo());
}

const PluginFactory::PluginInfo PluginFactory::pluginInfo(const char* name) const
{
for (const PluginInfo* info : m_pluginInfos)
for (const PluginInfo& info : m_pluginInfos)
{
if (qstrcmp(info->descriptor->name, name) == 0)
return *info;
if (qstrcmp(info.descriptor->name, name) == 0)
return info;
}
return PluginInfo();
}
Expand Down Expand Up @@ -150,7 +149,7 @@ void PluginFactory::discoverPlugins()

for (const QFileInfo& file : files)
{
QLibrary* library = new QLibrary(file.absoluteFilePath());
auto library = std::make_shared<QLibrary>(file.absoluteFilePath());

if (! library->load()) {
m_errors[file.baseName()] = library->errorString();
Expand All @@ -167,34 +166,28 @@ void PluginFactory::discoverPlugins()
descriptorName = descriptorName.mid(3);
}

Plugin::Descriptor* pluginDescriptor = (Plugin::Descriptor*) library->resolve(descriptorName.toUtf8().constData());
Plugin::Descriptor* pluginDescriptor = reinterpret_cast<Plugin::Descriptor*>(library->resolve(descriptorName.toUtf8().constData()));
if(pluginDescriptor == nullptr)
{
qWarning() << qApp->translate("PluginFactory", "LMMS plugin %1 does not have a plugin descriptor named %2!").
arg(file.absoluteFilePath()).arg(descriptorName);
continue;
}

PluginInfo* info = new PluginInfo;
info->file = file;
info->library = library;
info->descriptor = pluginDescriptor;
PluginInfo info;
info.file = file;
info.library = library;
info.descriptor = pluginDescriptor;
pluginInfos << info;

for (const QString& ext : QString(info->descriptor->supportedFileTypes).split(','))
for (const QString& ext : QString(info.descriptor->supportedFileTypes).split(','))
{
m_pluginByExt.insert(ext, info);
}

descriptors.insert(info->descriptor->type, info->descriptor);
descriptors.insert(info.descriptor->type, info.descriptor);
}


for (PluginInfo* info : m_pluginInfos)
{
delete info->library;
delete info;
}
m_pluginInfos = pluginInfos;
m_descriptors = descriptors;
}
Expand Down
24 changes: 9 additions & 15 deletions src/core/Song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Song::Song() :
m_playing( false ),
m_paused( false ),
m_loadingProject( false ),
m_errors( new QList<QString>() ),
m_playMode( Mode_None ),
m_length( 0 ),
m_patternToPlay( NULL ),
Expand Down Expand Up @@ -1138,12 +1137,12 @@ void Song::loadProject( const QString & fileName )
{
if ( gui )
{
QMessageBox::warning( NULL, tr("LMMS Error report"), *errorSummary(),
QMessageBox::warning( NULL, tr("LMMS Error report"), errorSummary(),
QMessageBox::Ok );
}
else
{
QTextStream(stderr) << *Engine::getSong()->errorSummary() << endl;
QTextStream(stderr) << Engine::getSong()->errorSummary() << endl;
}
}

Expand Down Expand Up @@ -1515,36 +1514,31 @@ void Song::removeController( Controller * controller )

void Song::clearErrors()
{
m_errors->clear();
m_errors.clear();
}



void Song::collectError( const QString error )
{
m_errors->append( error );
m_errors.append( error );
}



bool Song::hasErrors()
{
return ( m_errors->length() > 0 );
return ( m_errors.length() > 0 );
}



QString* Song::errorSummary()
QString Song::errorSummary()
{
QString* errors = new QString();
QString errors = m_errors.join("\n") + '\n';

for ( int i = 0 ; i < m_errors->length() ; i++ )
{
errors->append( m_errors->value( i ) + "\n" );
}

errors->prepend( "\n\n" );
errors->prepend( tr( "The following errors occured while loading: " ) );
errors.prepend( "\n\n" );
errors.prepend( tr( "The following errors occured while loading: " ) );

return errors;
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/editors/SongEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ SongEditor::SongEditor( Song * song ) :
m_smoothScroll( ConfigManager::inst()->value( "ui", "smoothscroll" ).toInt() ),
m_mode(DrawMode)
{
m_zoomingModel->setParent(this);
// create time-line
int widgetTotal = ConfigManager::inst()->value( "ui",
"compacttrackbuttons" ).toInt()==1 ?
Expand Down

0 comments on commit 24d320d

Please sign in to comment.