Skip to content

Commit

Permalink
fix more memory leaks small and big
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhendricks committed Feb 28, 2019
1 parent 85f78d7 commit cb9b833
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/MainUI/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/************************************************************************
**
** Copyright (C) 2016 Kevin B. Hendricks, Stratford, Ontario Canada
** Copyright (C) 2016, 2017, 2018, 2019 Kevin B. Hendricks, Stratford, Ontario Canada
** Copyright (C) 2012-2015 John Schember <john@nachtimwald.com>
** Copyright (C) 2012-2013 Dave Heiland
** Copyright (C) 2009-2011 Strahinja Markovic <strahinja.markovic@gmail.com>
Expand Down Expand Up @@ -4521,6 +4521,8 @@ void MainWindow::ExtendUI()

void MainWindow::UpdateClipButton(int clip_number, QAction *ui_action)
{
// clipEntry is a simple struct created by GetEntry with new,
// no reference counting or smart pointers so they must be cleaned up appropriately
ClipEditorModel::clipEntry *clip_entry = ClipEditorModel::instance()->GetEntryFromNumber(clip_number);

if (clip_entry) {
Expand All @@ -4529,6 +4531,8 @@ void MainWindow::UpdateClipButton(int clip_number, QAction *ui_action)
clip_text.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;");
ui_action->setToolTip(clip_text);
ui_action->setVisible(true);
// prevent memory leak
delete clip_entry;
} else {
ui_action->setText("");
ui_action->setToolTip("");
Expand Down
21 changes: 20 additions & 1 deletion src/Misc/CSSInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/************************************************************************
**
** Copyright (C) 2016 Kevin B. Hendricks, Stratford, ON Canada
** Copyright (C) 2016, 2017, 2018, 2019 Kevin B. Hendricks, Stratford, ON Canada
** Copyright (C) 2012 John Schember <john@nachtimwald.com>
** Copyright (C) 2012 Dave Heiland
** Copyright (C) 2012 Grant Drake
Expand Down Expand Up @@ -32,6 +32,11 @@ const int TAB_SPACES_WIDTH = 4;
const QString LINE_MARKER("[SIGIL_NEWLINE]");
static const QString DELIMITERS = "}{;";


// Note: CSSProperties and CSSSelectors are simple struct that this code
// created with new and so need to be manually cleaned up to prevent
// large memory leaks

CSSInfo::CSSInfo(const QString &text, bool isCSSFile)
: m_OriginalText(text),
m_IsCSSFile(isCSSFile)
Expand All @@ -52,6 +57,15 @@ CSSInfo::CSSInfo(const QString &text, bool isCSSFile)
}
}

// Need to manually clean up the Selector List
CSSInfo::~CSSInfo()
{
foreach(CSSSelector * sp, m_CSSSelectors) {
if (sp) delete sp;
}
}


QList<CSSInfo::CSSSelector *> CSSInfo::getClassSelectors(const QString filterClassName)
{
QList<CSSInfo::CSSSelector *> selectors;
Expand Down Expand Up @@ -156,6 +170,7 @@ QStringList CSSInfo::getAllPropertyValues(QString property)
if (property.isEmpty() || p->name == property) {
property_values.append(p->value);
}
delete p;
}
}

Expand Down Expand Up @@ -233,6 +248,10 @@ QString CSSInfo::getReformattedCSSText(bool multipleLineFormat)
QList<CSSInfo::CSSProperty *> new_properties = getCSSProperties(m_OriginalText, cssSelector->openingBracePos + 1, cssSelector->closingBracePos);
const QString &new_properties_text = formatCSSProperties(new_properties, multipleLineFormat, selector_indent);
new_text.replace(cssSelector->openingBracePos + 1, cssSelector->closingBracePos - cssSelector->openingBracePos - 1, new_properties_text);
// clear up new_properties as they were created with new
foreach(CSSInfo::CSSProperty* p, new_properties) {
if (p) delete p;
}
// Reformat the selector text itself - whitespace only since incomplete parsing.
// Will ensure the braces are placed on the same line as the selector name,
// comma separated groups are spaced apart and double spaces are removed.
Expand Down
3 changes: 3 additions & 0 deletions src/Misc/CSSInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/************************************************************************
**
** Copyright (C) 2019 Kevin B. Hendricks, Stratford, ON, Canada
** Copyright (C) 2012 John Schember <john@nachtimwald.com>
** Copyright (C) 2012 Dave Heiland
** Copyright (C) 2012 Grant Drake
Expand Down Expand Up @@ -41,6 +42,8 @@ class CSSInfo : public QObject
*/
CSSInfo(const QString &text, bool isCSSFile = true);

~CSSInfo();

struct CSSSelector {
QString originalText; /* The original text of the complete selector */
bool isGroup; /* Whether selector was declared in a comma separated group */
Expand Down
3 changes: 3 additions & 0 deletions src/ViewEditors/CodeViewEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,9 @@ void CodeViewEditor::FormatStyle(const QString &property_name, const QString &pr
} else {
property_values.append(QString("%1: %2").arg(css_property->name).arg(css_property->value));
}
// CSSInfo.getCSSProperties creates each CSSProperty pointer with new
// and it must be cleaned by caller to prevent memory leak
if (css_property) delete css_property;
}
style_attribute_value = QString("%1;").arg(property_values.join("; "));
}
Expand Down

1 comment on commit cb9b833

@kevinhendricks
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #397
For valgrind and leaks stack traces

Please sign in to comment.