Skip to content

Commit

Permalink
Fixed memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizbee committed Feb 21, 2020
1 parent cfb90ca commit 99635b9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
42 changes: 36 additions & 6 deletions src/limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ Limits::Limits(bool original) :
}
}

Limits::~Limits()
{
// Delete original
if (original_ != nullptr) {
delete original_;
}
}

Limits &Limits::operator=(const Limits &other)
{
// Copy limits
set(other.left(), other.right(), other.top(), other.bottom());

// Deep copy original limits
if (original_ != nullptr) {
setOriginal(
other.original()->left(),
other.original()->right(),
other.original()->top(),
other.original()->bottom());
}
return *this;
}

bool Limits::operator==(const Limits &other) const
{
// Check if limits are the same
Expand Down Expand Up @@ -106,6 +130,12 @@ void Limits::set(double left, double right, double top, double bottom)
bottom_ = bottom;
}

void Limits::setOriginal(double left, double right, double top, double bottom)
{
// Set original limits
original_->set(left, right, top, bottom);
}

double Limits::width() const
{
// Return width
Expand Down Expand Up @@ -148,12 +178,6 @@ QVector4D Limits::vec4() const
return QVector4D(top_, right_, bottom_, left_);
}

Limits *Limits::original()
{
// Return pointer to original limits
return original_;
}

double Limits::zoomFactor() const
{
// Return zoomFactor
Expand All @@ -172,3 +196,9 @@ void Limits::setZoomFactor(double zoomFactor)
top_ = yMid + h2;
bottom_ = yMid - h2;
}

const Limits *Limits::original() const
{
// Return pointer to original limits
return original_;
}
5 changes: 4 additions & 1 deletion src/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ class Limits
{
public:
Limits(bool original = false);
~Limits();
Limits &operator=(const Limits &other);
bool operator==(const Limits &other) const;
bool operator!=(const Limits &other) const;
void move(QPoint distance, const QSize &ref);
void zoom(bool in, double xw, double yw);
void reset(QSize size);
void resize(QSize delta);
void set(double left, double right, double top, double bottom);
void setOriginal(double left, double right, double top, double bottom);

double width() const;
double height() const;
Expand All @@ -29,9 +32,9 @@ class Limits
double top() const;
double bottom() const;
QVector4D vec4() const;
Limits *original();
double zoomFactor() const;
void setZoomFactor(double zoomFactor);
const Limits *original() const;

private:
double left_;
Expand Down
9 changes: 4 additions & 5 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ inline void iterateX(ImageLine &il)
{
// Iterate x-pixels
const quint8 rootCount = il.params->roots.count();
const Limits limits = il.params->limits;
const double xFactor = limits.width() / (il.lineSize - 1);
const double left = il.params->limits.left();
const double xFactor = il.params->limits.width() / (il.lineSize - 1);
const complex d = il.params->damping;

for (int x = 0; x < il.lineSize; ++x) {

// Create complex number from current pixel
il.zx = x * xFactor + limits.left();
il.zx = x * xFactor + left;
complex z(il.zx, il.zy);

// Newton iteration
Expand Down Expand Up @@ -158,8 +158,7 @@ void Renderer::renderFractal()
for (int y = 0; y < height; ++y) {
ImageLine il((QRgb*)(image->scanLine(y)), y, image->width(), &curParams_);
il.zy = y * yFactor + curParams_.limits.top();
// lines->append(il);
(*lines)[y] = il; // <- Preallocation requires a default ImageLine()
(*lines)[y] = il;
}

// Set thread count to either single or multicore
Expand Down
2 changes: 1 addition & 1 deletion src/settingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void SettingsWidget::importSettings()
params_->limits.set(
ini.value("left", 1).toDouble(), ini.value("right", 1).toDouble(),
ini.value("top", 1).toDouble(), ini.value("bottom", 1).toDouble());
params_->limits.original()->set(
params_->limits.setOriginal(
ini.value("left_original", 1).toDouble(), ini.value("right_original", 1).toDouble(),
ini.value("top_original", 1).toDouble(), ini.value("bottom_original", 1).toDouble());
ini.endGroup();
Expand Down

0 comments on commit 99635b9

Please sign in to comment.