Ticket #6856: weather_leaks.patch

File weather_leaks.patch, 18.6 KB (added by Marc Alban <marcalban@…>, 15 years ago)

Patch to fix memory leaks in MythWeather?

  • mythplugins/mythweather/mythweather/weather.cpp

     
    152152            {
    153153                mainStack->AddScreen(ssetup);
    154154            }
     155            else
     156            {
     157                delete ssetup;
     158            }
    155159
    156160            m_firstSetup = false;
    157161        }
     
    169173            units_t units = db.value(2).toUInt();
    170174            uint draworder = db.value(3).toUInt();
    171175
    172             ScreenListInfo *screenListInfo = m_allScreens[container];
     176            ScreenListInfo &screenListInfo = m_allScreens[container];
    173177
    174             WeatherScreen *ws = WeatherScreen::loadScreen(m_weatherStack, screenListInfo, id);
     178            WeatherScreen *ws = WeatherScreen::loadScreen(m_weatherStack, &screenListInfo, id);
    175179            if (!ws->Create())
     180            {
     181                delete ws;
    176182                continue;
     183            }
    177184
    178185            ws->setUnits(units);
    179186            ws->setInUse(true);
     
    319326        clearScreens();
    320327        mainStack->AddScreen(ssetup);
    321328    }
     329    else
     330    {
     331        delete ssetup;
     332    }
    322333
    323334    m_firstRun = true;
    324335}
     
    351362{
    352363    WeatherScreen *nxt = nextScreen();
    353364
    354     if (nxt->canShowScreen())
     365    if (nxt && nxt->canShowScreen())
    355366    {
    356367        hideScreen();
    357368        showScreen(nxt);
  • mythplugins/mythweather/mythweather/weatherScreen.cpp

     
    2525}
    2626
    2727WeatherScreen::WeatherScreen(MythScreenStack *parent, const QString &name,
    28                              ScreenListInfo *screenDefn, int id)
    29     : MythScreenType (parent, name)
     28                             ScreenListInfo *screenDefn, int id) :
     29    MythScreenType (parent, name),
     30    m_screenDefn(screenDefn),
     31    m_name(m_screenDefn->name),
     32    m_inuse(false),
     33    m_prepared(false),
     34    m_id(id)
    3035{
    31     m_screenDefn = screenDefn;
    32     m_name = m_screenDefn->name;
    33     m_id = id;
    34     m_prepared = false;
    3536
    36     m_inuse = false;
    37 
    3837    QStringList types = m_screenDefn->dataTypes;
    3938
    4039    for (int i = 0; i < types.size(); ++i)
  • mythplugins/mythweather/mythweather/sourceManager.h

     
    2121
    2222  public:
    2323    SourceManager();
     24    ~SourceManager();
    2425    WeatherSource *needSourceFor(int id, const QString &loc, units_t units);
    2526    QStringList getLocationList(ScriptInfo *si, const QString &str);
    2627    void startTimers();
  • mythplugins/mythweather/mythweather/weatherUtils.cpp

     
    6767        {
    6868            if ( (e.tagName() == "screen") && !screens.contains(e.attribute("name")) )
    6969            {
    70                 ScreenListInfo *screendef = new ScreenListInfo();
    71                 screendef->multiLoc = false;
    72                 screendef->name = e.attribute("name");
     70                screens[e.attribute("name")].multiLoc = false;
     71                screens[e.attribute("name")].name = e.attribute("name");
    7372                QString hasUnits = e.attribute("hasunits");
    7473                if (hasUnits.toLower() == "no")
    75                     screendef->hasUnits = false;
     74                    screens[e.attribute("name")].hasUnits = false;
    7675                else
    77                     screendef->hasUnits = true;
    78                 screendef->dataTypes = loadScreen(e);
    79                 screens[screendef->name] = screendef;
     76                    screens[e.attribute("name")].hasUnits = true;
     77                screens[e.attribute("name")].dataTypes = loadScreen(e);
    8078            }
    8179        }
    8280    }
  • mythplugins/mythweather/mythweather/weatherSetup.cpp

     
    110110        m_sourceManager->findScriptsDB();
    111111        m_sourceManager->setupSources();
    112112    }
     113
     114    // Deallocate the ScreenListInfo objects created for the inactive screen list.
     115    for (int i=0; i < m_inactiveList->GetCount(); i++)
     116    {
     117        MythUIButtonListItem *item = m_inactiveList->GetItemAt(i);
     118        if (item->GetData().isValid())
     119            delete qVariantValue<ScreenListInfo *>(item->GetData());
     120    }
     121
     122    // Deallocate the ScreenListInfo objects created for the active screen list.
     123    for (int i=0; i < m_activeList->GetCount(); i++)
     124    {
     125        MythUIButtonListItem *item = m_activeList->GetItemAt(i);
     126        if (item->GetData().isValid())
     127            delete qVariantValue<ScreenListInfo *>(item->GetData());
     128    }
    113129}
    114130
    115131bool ScreenSetup::Create()
     
    264280
    265281    QStringList types;
    266282
    267     ScreenListMap m_ScreenListMap = loadScreens();
     283    ScreenListMap screenListMap = loadScreens();
    268284
    269     ScreenListMap::const_iterator i = m_ScreenListMap.constBegin();
    270     while (i != m_ScreenListMap.constEnd())
     285    // Fill the inactive screen button list.
     286    ScreenListMap::const_iterator i = screenListMap.constBegin();
     287    while (i != screenListMap.constEnd())
    271288    {
    272289
    273         si = m_ScreenListMap[i.key()];
     290        si = &screenListMap[i.key()];
    274291        types = si->dataTypes;
    275292        si->units = ENG_UNITS;
    276293
     
    295312            }
    296313            MythUIButtonListItem *item =
    297314                    new MythUIButtonListItem(m_inactiveList, i.key());
    298             item->SetData(qVariantFromValue(si));
     315            item->SetData(qVariantFromValue(new ScreenListInfo(*si)));
    299316        }
    300317
    301318        ++i;
     
    320337        return;
    321338    }
    322339
     340    // Fill the active screen button list.
    323341    while (db.next())
    324342    {
    325343        QString name = db.value(0).toString();
     
    329347        QString src = db.value(4).toString();
    330348        uint draworder = db.value(5).toUInt();
    331349
    332         si = m_ScreenListMap[name];
    333         // Clear types first as we will re-insert the values from the database
    334         si->types.clear();
    335         types = si->dataTypes;
     350        types = screenListMap[name].dataTypes;
    336351
    337352        TypeListInfo ti(dataitem, location,
    338353                        m_sourceManager->getSourceByName(src));
    339354
    340355        if (active_screens.find(draworder) == active_screens.end())
    341356        {
     357            si = new ScreenListInfo(screenListMap[name]);
     358            // Clear types first as we will re-insert the values from the database
     359            si->types.clear();
     360            si->units = units;
     361           
    342362            MythUIButtonListItem *item =
    343363                new MythUIButtonListItem(m_activeList, name);
    344             si->units = units;
     364           
    345365
    346366            // Only insert types meant for this screen
    347367            for (QStringList::Iterator type_i = types.begin();
     
    492512                                                    "screensetupmenupopup");
    493513
    494514        if (menuPopup->Create())
     515        {
    495516            popupStack->AddScreen(menuPopup);
    496517
    497         menuPopup->SetReturnEvent(this, "options");
     518            menuPopup->SetReturnEvent(this, "options");
    498519
    499         menuPopup->AddButton(tr("Move Up"), qVariantFromValue(selected));
    500         menuPopup->AddButton(tr("Move Down"), qVariantFromValue(selected));
    501         menuPopup->AddButton(tr("Remove"), qVariantFromValue(selected));
    502         menuPopup->AddButton(tr("Change Location"), qVariantFromValue(selected));
    503         if (si->hasUnits)
    504             menuPopup->AddButton(tr("Change Units"), qVariantFromValue(selected));
    505         menuPopup->AddButton(tr("Cancel"), qVariantFromValue(selected));
     520            menuPopup->AddButton(tr("Move Up"), qVariantFromValue(selected));
     521            menuPopup->AddButton(tr("Move Down"), qVariantFromValue(selected));
     522            menuPopup->AddButton(tr("Remove"), qVariantFromValue(selected));
     523            menuPopup->AddButton(tr("Change Location"), qVariantFromValue(selected));
     524            if (si->hasUnits)
     525                menuPopup->AddButton(tr("Change Units"), qVariantFromValue(selected));
     526            menuPopup->AddButton(tr("Cancel"), qVariantFromValue(selected));
     527        }
     528        else
     529        {
     530            delete menuPopup;
     531        }
    506532
    507533    }
    508534    else if (GetFocusWidget() == m_inactiveList)
     
    522548        QList<ScriptInfo *> tmp;
    523549        if (m_sourceManager->findPossibleSources(type_strs, tmp))
    524550        {
    525             ScreenListInfo *newsi = new ScreenListInfo(*si);
    526 
    527551            if (!m_inactiveList->GetCount())
    528552            {
    529553                m_inactiveList->SetActive(false);
    530554                NextPrevWidgetFocus(true);
    531555            }
    532556            if (hasUnits)
    533                 showUnitsPopup(selected->GetText(), newsi);
     557                showUnitsPopup(selected->GetText(), si);
    534558            else
    535                 doLocationDialog(newsi);
     559                doLocationDialog(si);
    536560        }
    537561        else
    538562            VERBOSE(VB_IMPORTANT, "Screen cannot be used, not all required "
     
    550574
    551575    if (locdialog->Create())
    552576        mainStack->AddScreen(locdialog);
     577    else
     578            delete locdialog;
    553579}
    554580
    555581void ScreenSetup::showUnitsPopup(const QString &name, ScreenListInfo *si)
     
    565591                                                "weatherunitspopup");
    566592
    567593    if (menuPopup->Create())
     594    {
    568595        popupStack->AddScreen(menuPopup);
    569596
    570     menuPopup->SetReturnEvent(this, "units");
     597        menuPopup->SetReturnEvent(this, "units");
    571598
    572     menuPopup->AddButton(tr("English Units"), qVariantFromValue(si));
    573     menuPopup->AddButton(tr("SI Units"), qVariantFromValue(si));
     599        menuPopup->AddButton(tr("English Units"), qVariantFromValue(si));
     600        menuPopup->AddButton(tr("SI Units"), qVariantFromValue(si));
     601    }
     602    else
     603    {
     604        delete menuPopup;
     605    }
    574606}
    575607
    576608void ScreenSetup::deleteScreen()
    577609{
    578     if (m_activeList->GetItemCurrent())
    579         delete m_activeList->GetItemCurrent();
     610    MythUIButtonListItem *item = m_activeList->GetItemCurrent();
     611    if (item)
     612    {
     613        if (item->GetData().isValid())
     614            delete qVariantValue<ScreenListInfo *>(item->GetData());
    580615
     616        delete item;
     617    }
     618
    581619    if (!m_activeList->GetCount())
    582620    {
    583621        NextPrevWidgetFocus(false);
    584622        m_activeList->SetActive(false);
    585623    }
    586 
    587624}
    588625
    589626void ScreenSetup::customEvent(QEvent *event)
     
    632669            if (buttonnum > -1)
    633670            {
    634671                ScreenListInfo *si = qVariantValue<ScreenListInfo *>(dce->GetData());
     672                               
    635673                if (buttonnum == 0)
    636674                {
    637675                    si->units = ENG_UNITS;
     
    855893                               MythScreenType *retScreen, ScreenListInfo *si,
    856894                               SourceManager *srcman)
    857895    : MythScreenType(parent, name),
    858       m_screenListInfo(si),   m_sourceManager(srcman),
     896      m_screenListInfo(new ScreenListInfo(*si)),   m_sourceManager(srcman),
    859897      m_retScreen(retScreen), m_locationList(NULL),
    860898      m_locationEdit(NULL),   m_searchButton(NULL),
    861899      m_resultsText(NULL),    m_sourceText(NULL)
     
    868906
    869907LocationDialog::~LocationDialog()
    870908{
     909  if(m_locationList)
     910    clearResults();
     911   
     912  delete m_screenListInfo;
    871913}
    872914
    873915bool LocationDialog::Create()
     
    917959                                                       "mythweatherbusydialog");
    918960
    919961    if (busyPopup->Create())
     962    {
    920963        popupStack->AddScreen(busyPopup, false);
     964    }
     965    else
     966    {
     967        delete busyPopup;
     968        busyPopup = NULL;
     969    }
     970       
    921971
    922972    QMap<ScriptInfo *, QStringList> result_cache;
    923973    int numresults = 0;
    924     m_locationList->Reset();
     974    clearResults();
    925975
    926976    QString searchingresults = tr("Searching ... Results: %1");
    927977
     
    9781028    }
    9791029}
    9801030
     1031void LocationDialog::clearResults()
     1032{
     1033    for (int i=0; i < m_locationList->GetCount(); i++)
     1034    {
     1035        MythUIButtonListItem *item = m_locationList->GetItemAt(i);
     1036        if (item->GetData().isValid())
     1037            delete qVariantValue<ResultListInfo *>(item->GetData());
     1038    }
     1039   
     1040    m_locationList->Reset();
     1041}
     1042
    9811043void LocationDialog::itemSelected(MythUIButtonListItem *item)
    9821044{
    9831045    ResultListInfo *ri = qVariantValue<ResultListInfo *>(item->GetData());
     
    10021064
    10031065    DialogCompletionEvent *dce =
    10041066        new DialogCompletionEvent("location", 0, "",
    1005                                   qVariantFromValue(m_screenListInfo));
     1067                                  qVariantFromValue(new ScreenListInfo(*m_screenListInfo)));
    10061068    QApplication::postEvent(m_retScreen, dce);
    10071069
    10081070    Close();
  • mythplugins/mythweather/mythweather/weatherSource.cpp

     
    397397    }
    398398}
    399399
    400 WeatherSource::WeatherSource(const QString &filename)
    401     : m_ready(false),                  m_inuse(false),
    402       m_info(NULL),                    m_proc(NULL),
    403       m_dir(""),                       m_locale(""),
    404       m_units(SI_UNITS),
    405       m_scriptTimer(new QTimer(this)), m_updateTimer(new QTimer(this)),
    406       m_connectCnt(0)
    407 {
    408     connect( m_scriptTimer, SIGNAL(timeout()),
    409             this, SLOT(scriptTimeout()));
    410 
    411     connect( m_updateTimer, SIGNAL(timeout()),
    412             this, SLOT(updateTimeout()));
    413 
    414 
    415     const QFileInfo fi(filename);
    416     m_info = ProbeScript(fi);
    417 
    418     if (m_info)
    419     {
    420         m_proc = new QProcess();
    421         // program = filename;
    422         m_proc->setWorkingDirectory(
    423             QDir(GetShareDir() + "mythweather/scripts/").absolutePath());
    424         connect(this, SIGNAL(killProcess()),
    425                 m_proc, SLOT(kill()));
    426         m_ready = true;
    427     }
    428     else
    429         VERBOSE(VB_IMPORTANT, "Error probing script");
    430 }
    431 
    432400WeatherSource::~WeatherSource()
    433401{
    434402    delete m_proc;
  • mythplugins/mythweather/mythweather/main.cpp

     
    9898
    9999        if (gsetup->Create())
    100100            mainStack->AddScreen(gsetup);
     101        else
     102            delete gsetup;
    101103    }
    102104    else if (selection == "SETTINGS_SCREEN")
    103105    {
     
    105107
    106108        if (ssetup->Create())
    107109            mainStack->AddScreen(ssetup);
     110        else
     111            delete ssetup;
    108112    }
    109113    else if (selection == "SETTINGS_SOURCE")
    110114    {
     
    112116
    113117        if (srcsetup->Create())
    114118            mainStack->AddScreen(srcsetup);
     119        else
     120            delete srcsetup;
    115121//             MythPopupBox::showOkPopup(gContext->GetMainWindow(), "no sources",
    116122//                     QObject::tr("No Sources defined, Sources are defined by"
    117123//                                 " adding screens in Screen Setup."));
  • mythplugins/mythweather/mythweather/weatherSetup.h

     
    149149    void itemClicked(MythUIButtonListItem *item);
    150150
    151151  private:
     152    void clearResults();
     153
     154  private:
    152155    CacheMap m_cache;
    153156    QStringList m_types;
    154157    ScreenListInfo *m_screenListInfo;
  • mythplugins/mythweather/mythweather/weatherSource.h

     
    4646                          struct ScriptInfo &scriptInfo);
    4747
    4848    WeatherSource(ScriptInfo *info);
    49     WeatherSource(const QString &filename);
    5049    ~WeatherSource();
    5150
    5251    bool isReady() { return m_ready; }
  • mythplugins/mythweather/mythweather/weatherUtils.h

     
    2222class TypeListInfo
    2323{
    2424  public:
     25 
     26        TypeListInfo(const TypeListInfo& info)
     27            : name(info.name), location(info.location), src(info.src)
     28        {
     29            name.detach();
     30            location.detach();
     31        }
     32 
    2533    TypeListInfo(const QString &_name)
    2634        : name(_name), location(QString::null), src(NULL)
    2735    {
     
    5159class ScreenListInfo
    5260{
    5361  public:
     62    ScreenListInfo() {}
     63   
     64    ScreenListInfo(const ScreenListInfo& info) :
     65    name(info.name),
     66    types(info.types),
     67    helptxt(info.helptxt),
     68    sources(info.sources),
     69    units(info.units),
     70    hasUnits(info.hasUnits),
     71    multiLoc(info.multiLoc)
     72    {
     73      types.detach();
     74                }
     75   
    5476    TypeListInfo GetCurrentTypeList(void) const;
    5577
    5678  public:
     
    6688
    6789Q_DECLARE_METATYPE(ScreenListInfo *);
    6890
    69 typedef QMap<QString, ScreenListInfo *> ScreenListMap;
     91typedef QMap<QString, ScreenListInfo> ScreenListMap;
    7092
    7193ScreenListMap loadScreens();
    7294QStringList loadScreen(QDomElement ScreenListInfo);
  • mythplugins/mythweather/mythweather/sourceManager.cpp

     
    2727    setupSources();
    2828}
    2929
     30SourceManager::~SourceManager()
     31{
     32    clearSources();
     33}
     34
    3035bool SourceManager::findScriptsDB()
    3136{
    3237    MSqlQuery db(MSqlQuery::InitCon());
     
    9196                                                       "mythweatherbusydialog");
    9297
    9398    if (busyPopup->Create())
     99    {
    94100        popupStack->AddScreen(busyPopup, false);
     101    }
     102    else
     103    {
     104        delete busyPopup;
     105        busyPopup = NULL;
     106    }
    95107
    96108    qApp->processEvents();
    97109
     
    211223    if (!m_scripts.contains(si))
    212224        return QStringList();
    213225    WeatherSource *ws = new WeatherSource(si);
    214     return ws->getLocationList(str);
     226   
     227    QStringList locationList(ws->getLocationList(str));
     228   
     229    delete ws;
     230   
     231    return locationList;
    215232}
    216233
    217234WeatherSource *SourceManager::needSourceFor(int id, const QString &loc,