Skip to content

Commit

Permalink
Fix memory leak on startup (#1052)
Browse files Browse the repository at this point in the history
* Fix memory leaks for trikV62QtsGeneratorTest
* Fix leaking BrickFacktory
* Fix memleak in b2d wrapper
* Fix leaking QActions and few widgets
* TravisCI: use default LSAN_OPTIONS

Linux: docker requires additional capabilities:
google/sanitizers#764

macOS: detect_leaks=1 is unsupported as of Jul 2020

* Rename confusing imageRect()
* Remove strange oveloaded function,
we do not need selection when clicking outside of the box
* Repo: loadFromDisk always return correct empty repo
and no memory leaks occure

* Fix memleak when closing editor scene
When closing soon after opening, memory leaks and read-freed happen

* Avoid pointers to arrays
  • Loading branch information
iakov committed Aug 8, 2020
1 parent 5ce0790 commit bb46ce8
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,3 @@ QRectF RangeSensorItem::rect() const
{
return SensorItem::boundingRect();
}

void RangeSensorItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (!imageRect().contains(mouseEvent->pos())) {
setFlag(ItemIsMovable, false);
}

// TODO: Why not SensorItem::mousePressEvent ?
AbstractItem::mousePressEvent(mouseEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class RangeSensorItem : public SensorItem
QRectF rect() const override;

private:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;

/// Returns scanning region of a sensor as painter path, in relative to sensor coordinates.
QPainterPath scanningRegion() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SensorItem::SensorItem(model::SensorsConfiguration &configuration
, const PortInfo &port, const QString &pathToImage, const QRect &imageRect)
: mConfiguration(configuration)
, mPort(port)
, mImageRect(imageRect.isEmpty() ? this->imageRect() : imageRect)
, mImageRect(imageRect.isEmpty() ? this->calculateImageRect() : imageRect)
, mBoundingRect(mImageRect.adjusted(-selectionDrift, -selectionDrift
, selectionDrift, selectionDrift))
, mImage(pathToImage.isEmpty() ? this->pathToImage() : pathToImage, false)
Expand Down Expand Up @@ -182,7 +182,7 @@ QString SensorItem::name() const
}
}

QRectF SensorItem::imageRect() const
QRectF SensorItem::calculateImageRect() const
{
const DeviceInfo sensor = mConfiguration.type(mPort);
if (sensor.isA<robotParts::TouchSensor>()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SensorItem : public graphicsUtils::RotateItem, public items::SolidItem
const QRect mBoundingRect;
};

QRectF imageRect() const;
QRectF calculateImageRect() const;
QString name() const;
QString pathToImage() const;

Expand Down
3 changes: 2 additions & 1 deletion qrgui/mouseGestures/mouseMovementManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ MouseMovementManager::MouseMovementManager(const Id &diagram

// Initialization process is pretty long, so it must be performed in another thread.
// mInitializing flag will be set to false when initialization process is finished.
QtConcurrent::run(this, &MouseMovementManager::initializeGestures);
//QtConcurrent::run(this, &MouseMovementManager::initializeGestures);
MouseMovementManager::initializeGestures();
}

QWidget *MouseMovementManager::producePainter() const
Expand Down
11 changes: 6 additions & 5 deletions qrgui/mouseGestures/private/abstractRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ template <typename TKey>
class GesturesRecognizer : public GesturesManager
{
public:
using key_type = TKey;

GesturesRecognizer() = default;

~GesturesRecognizer() override = default;
Expand All @@ -56,9 +58,8 @@ class GesturesRecognizer : public GesturesManager

void initIdealGestures(QMap<QString, PathVector> const &objects) override
{
for (const QString &object : objects.keys()) {
TKey key = getKey(objects[object]);
mGestures.insert(object, key);
for (auto &&object : objects.keys()) {
mGestures.insert(object, getKey(objects[object]));
}
}

Expand All @@ -71,9 +72,9 @@ class GesturesRecognizer : public GesturesManager

protected:
TKey mKey;
virtual qreal getDistance(const TKey &key1, const TKey &key2) = 0;
virtual qreal getDistance(const key_type &key1, const key_type &key2) = 0;
virtual TKey getKey(const PathVector &path) = 0;
QMap<QString, TKey> mGestures;
QMap<QString, key_type> mGestures;
//maybe to do several lists for multistroke gestures
};

Expand Down
46 changes: 16 additions & 30 deletions qrgui/mouseGestures/private/mixedgesturesmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,9 @@ const qreal weight2 = 1 - weight1;

using namespace qReal::gestures;

MixedGesturesManager::MixedGesturesManager()
{
}

MixedGesturesManager::~MixedGesturesManager()
{
for (const QString &object : mGestures.keys()) {
delete[] mGestures[object].first;
delete[] mGestures[object].second;
mGestures.remove(object);
}
}
MixedGesturesManager::MixedGesturesManager() = default;

MixedGesturesManager::~MixedGesturesManager() = default;

qreal MixedGesturesManager::getMaxDistance(const QString &)
{
Expand All @@ -46,7 +36,7 @@ bool MixedGesturesManager::isMultistroke()
return true;
}

qreal MixedGesturesManager::getDistance(QPair<qreal *,qreal *> const &key1, QPair<qreal *, qreal *> const &key2)
qreal MixedGesturesManager::getDistance(const key_type &key1, const key_type &key2)
{
RectangleGesturesManager rectMan;
NearestPosGridGesturesManager gridMan;
Expand All @@ -55,16 +45,16 @@ qreal MixedGesturesManager::getDistance(QPair<qreal *,qreal *> const &key1, QPai
return dist1 * weight1 + dist2 * weight2;
}

QPair<qreal *, qreal *> MixedGesturesManager::getKey(const PathVector &path)
MixedGesturesManager::key_type MixedGesturesManager::getKey(const PathVector &path)
{
RectangleGesturesManager rectMan;
NearestPosGridGesturesManager gridMan;
qreal *key1 = rectMan.getKey(path);
qreal *key2 = gridMan.getKey(path);
return QPair<qreal *, qreal *>(key1, key2);
const auto &key1 = rectMan.getKey(path);
const auto &key2 = gridMan.getKey(path);
return {key1, key2};
}

MixedClassifier::MixedClassifier(QPair<qreal *, qreal *> &&key)
MixedClassifier::MixedClassifier(MixedGesturesManager::key_type &&key)
: mKey(key)
{
}
Expand All @@ -75,34 +65,30 @@ MixedClassifier::MixedClassifier(const PathVector &path)
mKey = gManager.getKey(path);
}

MixedClassifier::~MixedClassifier()
{
delete mKey.first;
delete mKey.second;
}
MixedClassifier::~MixedClassifier() = default;

qreal MixedClassifier::getDistance(const MixedClassifier &classifier)
{
QPair<qreal *, qreal *> key = classifier.key();
const auto &key = classifier.key();
MixedGesturesManager gManager;
return gManager.getDistance(key, mKey);
}

MixedClassifier MixedClassifier::getPoint(const MixedClassifier &centre, qreal centreWeight)
{
qreal *key1 = centre.key().first;
qreal *key2 = centre.key().second;
qreal *finalKey1 = new qreal[gridSize * gridSize];
qreal *finalKey2 = new qreal[gridSize * gridSize];
const auto &key1 = centre.key().first;
const auto &key2 = centre.key().second;
MixedGesturesManager::key_type::first_type finalKey1(gridSize * gridSize);
MixedGesturesManager::key_type::second_type finalKey2(gridSize * gridSize);
for (int i = 0; i < gridSize * gridSize; i ++) {
finalKey1[i] = (key1[i] * centreWeight + mKey.first[i]) / (centreWeight + 1);
finalKey2[i] = (key2[i] * centreWeight + mKey.second[i]) / (centreWeight + 1);
}

return MixedClassifier(QPair<qreal *, qreal *>(finalKey1, finalKey2));
return MixedClassifier(MixedGesturesManager::key_type {finalKey1, finalKey2});
}

QPair<qreal *, qreal *> MixedClassifier::key() const
MixedGesturesManager::key_type MixedClassifier::key() const
{
return mKey;
}
12 changes: 6 additions & 6 deletions qrgui/mouseGestures/private/mixedgesturesmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace qReal {
namespace gestures {

class MixedGesturesManager : public GesturesRecognizer<QPair<qreal *, qreal *> >
class MixedGesturesManager : public GesturesRecognizer<QPair<QVector<qreal>, QVector<qreal>>>
{
public:
MixedGesturesManager();
Expand All @@ -35,8 +35,8 @@ class MixedGesturesManager : public GesturesRecognizer<QPair<qreal *, qreal *> >
return getDistance(mKey, key);
}

qreal getDistance(QPair<qreal *, qreal *> const &key1, QPair<qreal *, qreal *> const &key2) override;
QPair<qreal *, qreal *> getKey(const PathVector &path) override;
qreal getDistance(const key_type &key1, const key_type &key2) override;
key_type getKey(const PathVector &path) override;
};

class MixedClassifier
Expand All @@ -50,12 +50,12 @@ class MixedClassifier

qreal getDistance(const MixedClassifier &classifier);
MixedClassifier getPoint(const MixedClassifier &centre, qreal centreWeight);
QPair<qreal *, qreal *> key() const;
MixedGesturesManager::key_type key() const;

private:
explicit MixedClassifier(QPair<qreal *, qreal *> &&key);
explicit MixedClassifier(MixedGesturesManager::key_type &&key);

QPair<qreal *, qreal *> mKey;
MixedGesturesManager::key_type mKey;
};

}
Expand Down
13 changes: 5 additions & 8 deletions qrgui/mouseGestures/private/nearestposgridgesturesmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ NearestPosGridGesturesManager::NearestPosGridGesturesManager()
{
}

NearestPosGridGesturesManager::~NearestPosGridGesturesManager()
{
qDeleteAll(mGestures);
}
NearestPosGridGesturesManager::~NearestPosGridGesturesManager() = default;

qreal NearestPosGridGesturesManager::getMaxDistance(const QString &)
{
Expand All @@ -37,7 +34,7 @@ bool NearestPosGridGesturesManager::isMultistroke()
return true;
}

qreal NearestPosGridGesturesManager::getDistance(qreal * const &key1, qreal * const &key2)
qreal NearestPosGridGesturesManager::getDistance(const key_type &key1, const key_type &key2)
{
qreal norm = 0;
qreal sum = 0;
Expand All @@ -49,10 +46,10 @@ qreal NearestPosGridGesturesManager::getDistance(qreal * const &key1, qreal * co
return norm + sum / (gridSize * gridSize);
}

qreal *NearestPosGridGesturesManager::getKey(const PathVector &path)
NearestPosGridGesturesManager::key_type NearestPosGridGesturesManager::getKey(const PathVector &path)
{
Key key = KeyBuilder::getKey(path, gridSize, gridSize);
qreal * finalKey = new qreal[gridSize * gridSize]; // deal with this too
const auto &key = KeyBuilder::getKey(path, gridSize, gridSize);
key_type finalKey (gridSize * gridSize);
for (int i = 0; i < gridSize * gridSize; ++i) {
finalKey[i] = gridSize;
}
Expand Down
6 changes: 3 additions & 3 deletions qrgui/mouseGestures/private/nearestposgridgesturesmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
namespace qReal {
namespace gestures {

class NearestPosGridGesturesManager : public GesturesRecognizer<qreal *>
class NearestPosGridGesturesManager : public GesturesRecognizer<QVector<qreal>>
{
public:
NearestPosGridGesturesManager();
~NearestPosGridGesturesManager() override;
qreal getMaxDistance(const QString &) override;
bool isMultistroke() override;
qreal getDistance(qreal * const & key1, qreal * const &key2) override;
qreal getDistance(const key_type &key1, const key_type &key2) override;

qreal getDistance(QString const &item) override
{
const auto key = mGestures[item];
return getDistance(mKey, key);
}

qreal *getKey(const PathVector &path) override;
key_type getKey(const PathVector &path) override;
};

}
Expand Down
15 changes: 5 additions & 10 deletions qrgui/mouseGestures/private/rectanglegesturesmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@

using namespace qReal::gestures;

RectangleGesturesManager::RectangleGesturesManager()
{
}
RectangleGesturesManager::RectangleGesturesManager() = default;

RectangleGesturesManager::~RectangleGesturesManager()
{
qDeleteAll(mGestures);
}
RectangleGesturesManager::~RectangleGesturesManager() = default;

qreal RectangleGesturesManager::getMaxDistance(const QString &)
{
Expand All @@ -38,7 +33,7 @@ bool RectangleGesturesManager::isMultistroke()
return true;
}

qreal RectangleGesturesManager::getDistance(qreal * const & key1, qreal * const & key2)
qreal RectangleGesturesManager::getDistance(const key_type &key1, const key_type &key2)
{
qreal norm = 0;
qreal sum = 0;
Expand All @@ -50,10 +45,10 @@ qreal RectangleGesturesManager::getDistance(qreal * const & key1, qreal * const
return sum / (gridSize * gridSize);
}

qreal *RectangleGesturesManager::getKey(const PathVector & path)
RectangleGesturesManager::key_type RectangleGesturesManager::getKey(const PathVector & path)
{
const Key key = KeyBuilder::getKey(path, gridSize, gridSize);
qreal *finalKey = new qreal[gridSize * gridSize];
key_type finalKey (gridSize * gridSize);
for (int i = 0; i < gridSize * gridSize; ++i) {
finalKey[i] = key.size();
}
Expand Down
6 changes: 3 additions & 3 deletions qrgui/mouseGestures/private/rectanglegesturesmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
namespace qReal {
namespace gestures {

class RectangleGesturesManager : public GesturesRecognizer<qreal *>
class RectangleGesturesManager : public GesturesRecognizer<QVector<qreal>>
{
public:
RectangleGesturesManager();
~RectangleGesturesManager() override;
qreal getMaxDistance(const QString &) override;
bool isMultistroke() override;
qreal getDistance(qreal * const &key1, qreal * const &key2) override;
qreal getDistance(const key_type &key1, const key_type &key2) override;

qreal getDistance(QString const &item) override
{
const auto key = mGestures[item];
return getDistance(mKey, key);
}

qreal *getKey(const PathVector & path) override;
key_type getKey(const PathVector & path) override;
};

}
Expand Down
Loading

0 comments on commit bb46ce8

Please sign in to comment.