Skip to content

Commit

Permalink
Tidy up memory allocation and remove leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwbnwif committed Nov 13, 2024
1 parent 61222d1 commit 3fbc44b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
42 changes: 28 additions & 14 deletions qucs/components/componentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
6. DONE: Have "Export" as a check box, or option list for Qucsator equations.
7. DONE: .INCLUDE components have multiple files
8. Should 'Lib' parameters also be able to open a file?
9. Check for memory leaks.
9. DONE: Check for memory leaks.
*/

#include "componentdialog.h"
Expand All @@ -52,7 +52,7 @@ QStringList getOptionsFromString(const QString& description)
{
list = description.mid(start + 1, end - start - 1).split(',');
for(auto entry : list)
options << entry.trimmed();
options << entry.trimmed(); // QString::trimmed flagged by valgrind leak check
}

return options;
Expand Down Expand Up @@ -126,6 +126,11 @@ class ParamWidget
layout->addWidget(mCheckBox, row, 2);
}

virtual ~ParamWidget()
{
// qDebug() << "ParamWidget dtor called";
}

void setLabel(const QString& label)
{
mLabel->setText(label + ":");
Expand Down Expand Up @@ -178,14 +183,14 @@ class ParamLineEdit : public QLineEdit, public ParamWidget
{
public:
ParamLineEdit(const QString& param, const QString& label, QValidator* validator, bool displayCheck, QGridLayout* layout, ComponentDialog* dialog,
void (ComponentDialog::* func)(const QString&, const QString&) = nullptr)
void (ComponentDialog::* func)(const QString&) = nullptr)
: ParamWidget(param, label, displayCheck, layout)
{
layout->addWidget(this, layout->rowCount() - 1, 1);
setValidator(validator);

if (func)
connect(this, &QLineEdit::textEdited, [=](const QString& value) { if (dialog) (dialog->*func)(mParam, value); });
connect(this, &QLineEdit::textEdited, [=]() { if (dialog) (dialog->*func)(mParam); });
}

void setEnabled(bool enabled) override
Expand Down Expand Up @@ -219,13 +224,13 @@ class ParamCombo : public QComboBox, public ParamWidget
{
public:
ParamCombo(const QString& param, const QString& label, bool displayCheck, QGridLayout* layout, ComponentDialog* dialog,
void (ComponentDialog::* func)(const QString&, const QString&) = nullptr)
void (ComponentDialog::* func)(const QString&) = nullptr)
: ParamWidget(param, label, displayCheck, layout)
{
layout->addWidget(this, layout->rowCount() - 1, 1);

if (func)
connect(this, &QComboBox::currentTextChanged, [=](const QString& value) { if (dialog) (dialog->*func)(mParam, value); });
connect(this, &QComboBox::currentTextChanged, [=]() { if (dialog) (dialog->*func)(mParam); });
}

void setEnabled(bool enabled) override
Expand Down Expand Up @@ -255,10 +260,6 @@ class ParamCombo : public QComboBox, public ParamWidget
{
return currentText();
}

private:
void (ComponentDialog::* function)(const QString&, const QString&);
ComponentDialog* mDialog;
};

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -303,6 +304,11 @@ EqnHighlighter::EqnHighlighter(const QString& keywordSet, QTextDocument* parent)
highlightingRules.append(rule);
}

EqnHighlighter::~EqnHighlighter()
{
// qDebug() << "EqnHighlighter dtor called";
}

// -------------------------------------------------------------------------
// Sets up the syntax highlighter for the equation editor.
void EqnHighlighter::highlightBlock(const QString& text)
Expand Down Expand Up @@ -409,7 +415,7 @@ ComponentDialog::ComponentDialog(Component* schematicComponent, Schematic* schem
QGridLayout* sweepPageLayout = new QGridLayout(sweepPage);

// Sweep page setup - add widgets for each possible sweep property.
void (ComponentDialog::* func)(const QString&, const QString&) = &ComponentDialog::updateSweepProperty;
void (ComponentDialog::* func)(const QString&) = &ComponentDialog::updateSweepProperty;
sweepParamWidget["Sim"] = new ParamCombo("Sim", tr("Simulation"), true, sweepPageLayout, this, func);
sweepParamWidget["Param"] = new ParamLineEdit("Param", tr("Sweep Parameter"), compNameVal, true, sweepPageLayout, this, func);
sweepParamWidget["Type"] = new ParamCombo("Type", tr("Type"), true, sweepPageLayout, this, func);
Expand All @@ -429,7 +435,7 @@ ComponentDialog::ComponentDialog(Component* schematicComponent, Schematic* schem
// Setup the widgets as per the stored type.
sweepParamWidget["Sim"]->setOptions(getSimulationList());
sweepParamWidget["Type"]->setOptions({"lin", "log", "list", "value"});
updateSweepProperty("All", "");
updateSweepProperty("All");

// Create the properties page and add it to the tab widget.
QWidget* propertiesPage = new QWidget(pageTabs);
Expand All @@ -452,7 +458,7 @@ ComponentDialog::ComponentDialog(Component* schematicComponent, Schematic* schem
// Allow populating from a spice file if appropriate.
if (QStringList({"Diode", "_BJT", "JFET", "MOSFET"}).contains(component->Model))
{
QHBoxLayout *spiceButtonLayout = new QHBoxLayout(this);
QHBoxLayout *spiceButtonLayout = new QHBoxLayout;
propertyTableLayout->addLayout(spiceButtonLayout);
QPushButton* spiceButton = new QPushButton(tr("Populate parameters from SPICE file..."), this);
connect(spiceButton, &QPushButton::released, this, &ComponentDialog::slotFillFromSpice);
Expand Down Expand Up @@ -496,6 +502,14 @@ ComponentDialog::~ComponentDialog()
delete intVal;
delete nameVal;
delete paramVal;

// Clean up the sweep parameter multi-widget containers. The widgets are deleted by
// the system because they are reparented to the dialog or the dialog's subwidgets.
for (auto it = sweepParamWidget.keyValueBegin(); it != sweepParamWidget.keyValueEnd(); ++it)
{
if (it->second)
delete it->second;
}
}

// -------------------------------------------------------------------------
Expand All @@ -515,7 +529,7 @@ void ComponentDialog::updateSweepWidgets(const QString& type)

// -------------------------------------------------------------------------
// Updates all the sweep params on the sweep page according the component value.
void ComponentDialog::updateSweepProperty(const QString& property, const QString& value)
void ComponentDialog::updateSweepProperty(const QString& property)
{
// Type has changed so update the widget presentation.
if (property == "Type")
Expand Down
3 changes: 2 additions & 1 deletion qucs/components/componentdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private slots:

QStringList getSimulationList();

void updateSweepProperty(const QString& propertyWidget, const QString& value);
void updateSweepProperty(const QString& propertyWidget);
void updateSweepWidgets(const QString& simType);
void updatePropertyTable();
void updateEqnEditor();
Expand All @@ -116,6 +116,7 @@ Q_OBJECT

public:
EqnHighlighter(const QString& keywordSet, QTextDocument* parent);
~EqnHighlighter();

protected:
void highlightBlock(const QString &text) override;
Expand Down

0 comments on commit 3fbc44b

Please sign in to comment.