From df954ae483172016a3425ae3adee484e2cc5385d Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Mon, 14 Apr 2014 20:25:17 +0200 Subject: [PATCH] Fixed many memory leaks --- companion/src/companion.cpp | 2 ++ companion/src/eeprominterface.cpp | 16 +++++++++ companion/src/eeprominterface.h | 3 ++ .../src/firmwares/opentx/opentxeeprom.cpp | 33 ++++++++++++++++--- companion/src/firmwares/opentx/opentxeeprom.h | 1 + .../src/firmwares/opentx/opentxinterface.cpp | 7 ++++ .../src/firmwares/opentx/opentxinterface.h | 1 + companion/src/helpers.cpp | 5 ++- companion/src/mainwindow.cpp | 10 +++--- companion/src/mdichild.cpp | 4 +++ companion/src/mdichild.h | 1 + companion/src/simulation/simulatordialog.cpp | 1 + companion/src/simulation/simulatorinterface.h | 2 ++ 13 files changed, 76 insertions(+), 10 deletions(-) diff --git a/companion/src/companion.cpp b/companion/src/companion.cpp index 37a988273..ce0f282ae 100644 --- a/companion/src/companion.cpp +++ b/companion/src/companion.cpp @@ -123,5 +123,7 @@ int main(int argc, char *argv[]) delete splash; delete mainWin; + UnregisterFirmwares(); + return result; } diff --git a/companion/src/eeprominterface.cpp b/companion/src/eeprominterface.cpp index 1862e0ed5..d7594e9fb 100644 --- a/companion/src/eeprominterface.cpp +++ b/companion/src/eeprominterface.cpp @@ -5,6 +5,7 @@ #include "firmwares/th9x/th9xinterface.h" #include "firmwares/gruvin9x/gruvin9xinterface.h" #include "firmwares/opentx/opentxinterface.h" +#include "firmwares/opentx/opentxeeprom.h" #include "firmwares/ersky9x/ersky9xinterface.h" #include "appdata.h" #include "helpers.h" @@ -1202,6 +1203,15 @@ void RegisterEepromInterfaces() eepromInterfaces.push_back(new Er9xInterface()); } +void UnregisterEepromInterfaces() +{ + foreach(EEPROMInterface * intf, eepromInterfaces) { + //std::cout << "UnregisterEepromInterfaces(): deleting " << std::hex << reinterpret_cast(intf) << std::dec << std::endl; + delete intf; + } + OpenTxEepromCleanup(); +} + QList firmwares; FirmwareVariant default_firmware_variant; FirmwareVariant current_firmware_variant; @@ -1214,6 +1224,12 @@ void RegisterFirmwares() RegisterEepromInterfaces(); } +void UnregisterFirmwares() { + UnregisterEepromInterfaces(); + UnregisterOpen9xFirmwares(); +} + + bool LoadEeprom(RadioData &radioData, const uint8_t *eeprom, const int size) { foreach(EEPROMInterface *eepromInterface, eepromInterfaces) { diff --git a/companion/src/eeprominterface.h b/companion/src/eeprominterface.h index adecee4fb..be4428c38 100644 --- a/companion/src/eeprominterface.h +++ b/companion/src/eeprominterface.h @@ -1245,6 +1245,7 @@ inline void applyStickModeToModel(ModelData &model, unsigned int mode) } void RegisterFirmwares(); +void UnregisterFirmwares(); bool LoadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index); bool LoadEeprom(RadioData &radioData, const uint8_t *eeprom, int size); @@ -1269,6 +1270,8 @@ class FirmwareInfo { virtual ~FirmwareInfo() { + //std::cout << "~FirmwareInfo(): id: " << id.toUtf8().constData() << " name: " << name.toUtf8().constData() << std::endl; + if (eepromInterface) delete eepromInterface; } FirmwareInfo(const QString & id, const QString & name, EEPROMInterface * eepromInterface, const QString & url = QString(), const QString & stamp = QString(), const QString & rnurl = QString(), bool voice = false): diff --git a/companion/src/firmwares/opentx/opentxeeprom.cpp b/companion/src/firmwares/opentx/opentxeeprom.cpp index 588bb6562..6a210caaf 100644 --- a/companion/src/firmwares/opentx/opentxeeprom.cpp +++ b/companion/src/firmwares/opentx/opentxeeprom.cpp @@ -129,12 +129,12 @@ class SwitchesConversionTable: public ConversionTable { SwitchesConversionTable * table; }; + static std::list internalCache; + public: static SwitchesConversionTable * getInstance(BoardEnum board, unsigned int version, unsigned long flags=0) { - static std::list internalCache; - for (std::list::iterator it=internalCache.begin(); it!=internalCache.end(); it++) { Cache element = *it; if (element.board == board && element.version == version && element.flags == flags) @@ -145,8 +145,17 @@ class SwitchesConversionTable: public ConversionTable { internalCache.push_back(element); return element.table; } + static void Cleanup() { + for (std::list::iterator it=internalCache.begin(); it!=internalCache.end(); it++) { + Cache element = *it; + std::cout << "~SwitchesConversionTable(): deleting " << std::hex << reinterpret_cast(element.table) << std::dec << std::endl; + if (element.table) delete element.table; + } + } }; +std::list SwitchesConversionTable::internalCache; + #define FLAG_NONONE 0x01 #define FLAG_NOSWITCHES 0x02 #define FLAG_NOTELEMETRY 0x04 @@ -281,13 +290,12 @@ class SourcesConversionTable: public ConversionTable { unsigned long flags; SourcesConversionTable * table; }; + static std::list internalCache; public: static SourcesConversionTable * getInstance(BoardEnum board, unsigned int version, unsigned int variant, unsigned long flags=0) { - static std::list internalCache; - for (std::list::iterator it=internalCache.begin(); it!=internalCache.end(); it++) { Cache element = *it; if (element.board == board && element.version == version && element.variant == variant && element.flags == flags) @@ -298,8 +306,22 @@ class SourcesConversionTable: public ConversionTable { internalCache.push_back(element); return element.table; } + static void Cleanup() { + for (std::list::iterator it=internalCache.begin(); it!=internalCache.end(); it++) { + Cache element = *it; + //std::cout << "~SourcesConversionTable(): deleting " << std::hex << reinterpret_cast(element.table) << std::dec << std::endl; + if (element.table) delete element.table; + } + } }; +std::list SourcesConversionTable::internalCache; + +void OpenTxEepromCleanup(void){ + SourcesConversionTable::Cleanup(); + SwitchesConversionTable::Cleanup(); +} + ThrottleSourceConversionTable::ThrottleSourceConversionTable(BoardEnum board, unsigned int version) { int val=0; @@ -1436,6 +1458,9 @@ class LogicalSwitchField: public TransformedField { } } + ~LogicalSwitchField() { + if (andswitchesConversionTable) delete andswitchesConversionTable; + } virtual void beforeExport() { if (csw.func == LS_FN_TIMER) { diff --git a/companion/src/firmwares/opentx/opentxeeprom.h b/companion/src/firmwares/opentx/opentxeeprom.h index e830b6dfc..8d3d4cb60 100644 --- a/companion/src/firmwares/opentx/opentxeeprom.h +++ b/companion/src/firmwares/opentx/opentxeeprom.h @@ -144,4 +144,5 @@ class OpenTxModelData: public TransformedField { ChannelsConversionTable channelsConversionTable; }; +void OpenTxEepromCleanup(void); #endif diff --git a/companion/src/firmwares/opentx/opentxinterface.cpp b/companion/src/firmwares/opentx/opentxinterface.cpp index 0ef9eaf97..322eaebe2 100644 --- a/companion/src/firmwares/opentx/opentxinterface.cpp +++ b/companion/src/firmwares/opentx/opentxinterface.cpp @@ -1232,3 +1232,10 @@ void RegisterOpen9xFirmwares() firmwares.push_back(open9x); } } + +void UnregisterOpen9xFirmwares() { + foreach (FirmwareInfo * f, firmwares) { + //std::cout << "UnregisterOpen9xFirmwares(): deleting " << std::hex << reinterpret_cast(f) << std::dec << std::endl; + delete f; + } +} diff --git a/companion/src/firmwares/opentx/opentxinterface.h b/companion/src/firmwares/opentx/opentxinterface.h index 809b9f926..03bcf34f0 100644 --- a/companion/src/firmwares/opentx/opentxinterface.h +++ b/companion/src/firmwares/opentx/opentxinterface.h @@ -164,5 +164,6 @@ class Open9xFirmware: public FirmwareInfo { }; void RegisterOpen9xFirmwares(); +void UnregisterOpen9xFirmwares(); #endif diff --git a/companion/src/helpers.cpp b/companion/src/helpers.cpp index 92ec4bd87..17d296d35 100644 --- a/companion/src/helpers.cpp +++ b/companion/src/helpers.cpp @@ -2,6 +2,7 @@ #include "appdata.h" #include "helpers.h" #include "simulatordialog.h" +#include "simulatorinterface.h" #include "flashinterface.h" const QColor colors[C9X_MAX_CURVES] = { @@ -905,7 +906,9 @@ CompanionIcon::CompanionIcon(QString baseimage) void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) { - if (GetEepromInterface()->getSimulator()) { + SimulatorInterface * si = GetEepromInterface()->getSimulator(); + if (si) { + delete si; RadioData * simuData = new RadioData(radioData); unsigned int flags = 0; if (modelIdx >= 0) { diff --git a/companion/src/mainwindow.cpp b/companion/src/mainwindow.cpp index c80b19309..4aba18d93 100644 --- a/companion/src/mainwindow.cpp +++ b/companion/src/mainwindow.cpp @@ -1681,10 +1681,10 @@ void MainWindow::createActions() void MainWindow::createMenus() { - QMenu *recentFileMenu=new QMenu(tr("Recent Models+Settings")); - QMenu *languageMenu=new QMenu(tr("Set Menu Language")); - QMenu *themeMenu=new QMenu(tr("Set Icon Theme")); - QMenu *iconThemeSizeMenu=new QMenu(tr("Set Icon Size")); + QMenu *recentFileMenu=new QMenu(tr("Recent Models+Settings"), this); + QMenu *languageMenu=new QMenu(tr("Set Menu Language"), this); + QMenu *themeMenu=new QMenu(tr("Set Icon Theme"), this); + QMenu *iconThemeSizeMenu=new QMenu(tr("Set Icon Size"), this); fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(newAct); @@ -1783,7 +1783,7 @@ QMenu *MainWindow::createRecentFileMenu() QMenu *MainWindow::createProfilesMenu() { - QMenu *profilesMenu=new QMenu(tr("Radio Profile")); + QMenu *profilesMenu=new QMenu(tr("Radio Profile"), this); int i; for ( i = 0; i < MAX_PROFILES; ++i) { profilesMenu->addAction(profileActs[i]); diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index a401026ca..3be909f22 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -82,6 +82,10 @@ MdiChild::MdiChild(): } } +MdiChild::~MdiChild() { + delete ui; +} + void MdiChild::qSleep(int ms) { if (ms<0) diff --git a/companion/src/mdichild.h b/companion/src/mdichild.h index 1ff1730cc..fab250127 100644 --- a/companion/src/mdichild.h +++ b/companion/src/mdichild.h @@ -61,6 +61,7 @@ class MdiChild : public QWidget public: MdiChild(); + ~MdiChild(); void newFile(); bool loadFile(const QString &fileName, bool resetCurrentFile=true); diff --git a/companion/src/simulation/simulatordialog.cpp b/companion/src/simulation/simulatordialog.cpp index bc06c959e..1e13c2729 100644 --- a/companion/src/simulation/simulatordialog.cpp +++ b/companion/src/simulation/simulatordialog.cpp @@ -142,6 +142,7 @@ SimulatorDialogTaranis::~SimulatorDialogTaranis() SimulatorDialog::~SimulatorDialog() { delete timer; + if (simulator) delete simulator; } void SimulatorDialog::closeEvent (QCloseEvent *) diff --git a/companion/src/simulation/simulatorinterface.h b/companion/src/simulation/simulatorinterface.h index 5c75dbe91..65ee2792b 100644 --- a/companion/src/simulation/simulatorinterface.h +++ b/companion/src/simulation/simulatorinterface.h @@ -48,6 +48,8 @@ class SimulatorInterface { public: + virtual ~SimulatorInterface() {}; + virtual void start(QByteArray & eeprom, bool tests=true) = 0; virtual void start(const char * filename, bool tests=true) = 0;