Skip to content

Commit dc6e36b

Browse files
authored
Fix crash on exit + minor improvements (#1894)
* [trik-runtime] latest fixes ** Crash in script thread shutdown * Fix crash on exit + minor improvements ** Fixed problem with global QMap concurrent access
1 parent cf2b8eb commit dc6e36b

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

plugins/robots/common/kitBase/include/kitBase/blocksBase/common/deviceBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DeviceBlock : public RobotsBlock
3636

3737
void run() override
3838
{
39-
const robotModel::DeviceInfo deviceInfo = robotModel::DeviceInfo::create<Device>();
39+
const robotModel::DeviceInfo &deviceInfo = robotModel::DeviceInfo::create<Device>();
4040
const QString portProperty = eval<QString>("Port");
4141
const QString port = !portProperty.isEmpty() ? portProperty :
4242
deviceInfo.name()[0].toUpper() + deviceInfo.name().mid(1) + "Port";

plugins/robots/common/kitBase/include/kitBase/robotModel/deviceInfo.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <QtCore/QMetaObject>
2020
#include <QtCore/QMetaProperty>
2121

22+
#include <QReadWriteLock>
23+
2224
#include "direction.h"
2325
#include "kitBase/kitBaseDeclSpec.h"
2426

@@ -30,12 +32,15 @@ namespace robotModel {
3032
class ROBOTS_KIT_BASE_EXPORT DeviceInfo
3133
{
3234
public:
35+
/// Constructs invalid DeviceInfo instance.
36+
DeviceInfo();
37+
3338
/// Creates a new instance of a Device descriptor. The resulting object will
3439
/// correspond to a given type only if Q_OBJECT macro is used inside its declaration.
3540
/// @warning Given device type must contain friendlyName() and direction() static functions
3641
/// and Q_OBJECT macro.
3742
template <typename T>
38-
static DeviceInfo create()
43+
static DeviceInfo &create()
3944
{
4045
// This line performs Q_OBJECT macro checking in the given type declaration.
4146
// Without Q_OBJECT macro incorrect metaObject will be passed and it will lead
@@ -47,15 +52,13 @@ class ROBOTS_KIT_BASE_EXPORT DeviceInfo
4752
const bool simulated = property(metaObject, "simulated") == "true";
4853
const Direction direction = property(metaObject, "direction").toLower() == "input" ? input : output;
4954
DeviceInfo result(metaObject, name, friendlyName, simulated, direction);
50-
mCreatedInfos[QString(metaObject->className())] = result;
51-
return result;
55+
QWriteLocker w(&mRWLock);
56+
auto &r = mCreatedInfos[QString(metaObject->className())] = std::move(result);
57+
return r;
5258
}
5359

5460
/// Deserializes inner string representation obtained by toString().
55-
static DeviceInfo fromString(const QString &string);
56-
57-
/// Constructs invalid DeviceInfo instance.
58-
DeviceInfo();
61+
static DeviceInfo &fromString(const QString &string);
5962

6063
/// Serializes given device info into inner string representation.
6164
QString toString() const;
@@ -115,6 +118,7 @@ class ROBOTS_KIT_BASE_EXPORT DeviceInfo
115118

116119
static QString property(const QMetaObject * const metaObject, const QString &name);
117120

121+
static QReadWriteLock mRWLock;
118122
static QMap<QString, DeviceInfo> mCreatedInfos;
119123

120124
const QMetaObject *mDeviceType;

plugins/robots/common/kitBase/src/robotModel/deviceInfo.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
using namespace kitBase::robotModel;
2323

24-
QMap<QString, DeviceInfo> DeviceInfo::mCreatedInfos = QMap<QString, DeviceInfo>();
24+
QMap<QString, DeviceInfo> DeviceInfo::mCreatedInfos = {{QString(), DeviceInfo()}};
25+
QReadWriteLock DeviceInfo::mRWLock;
26+
2527

2628
DeviceInfo::DeviceInfo()
2729
: mDeviceType(nullptr)
@@ -90,17 +92,15 @@ QString DeviceInfo::toString() const
9092
return QString(mDeviceType ? mDeviceType->className() : QString());
9193
}
9294

93-
DeviceInfo DeviceInfo::fromString(const QString &string)
95+
DeviceInfo &DeviceInfo::fromString(const QString &string)
9496
{
95-
if (string.isEmpty()) {
96-
return DeviceInfo();
97-
}
98-
99-
if (!mCreatedInfos.contains(string)) {
97+
QReadLocker r(&mRWLock);
98+
auto i = mCreatedInfos.find(string);
99+
if (i == mCreatedInfos.end()) {
100100
throw qReal::Exception(QString("QMetaObject for %1 not found").arg(string));
101101
}
102102

103-
return mCreatedInfos[string];
103+
return *i;
104104
}
105105

106106
QString DeviceInfo::property(const QMetaObject * const metaObject, const QString &name)

plugins/robots/common/twoDModel/src/engine/model/sensorsConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void SensorsConfiguration::deserialize(const QDomElement &element)
128128

129129
const PortInfo port = PortInfo::fromString(sensorNode.attribute("port"));
130130

131-
const DeviceInfo type = DeviceInfo::fromString(sensorNode.attribute("type"));
131+
const DeviceInfo &type = DeviceInfo::fromString(sensorNode.attribute("type"));
132132

133133
const QString positionStr = sensorNode.attribute("position", "0:0");
134134
const QStringList splittedStr = positionStr.split(":");

plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void TrikBrick::reset()
7171
}
7272

7373
mTimers.clear();
74+
75+
Q_EMIT resetCompleted();
7476
}
7577

7678
void TrikBrick::printToShell(const QString &msg)
Submodule trikRuntime updated 196 files

qrtranslations/fr/plugins/robots/trikKitInterpreterCommon_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<context>
133133
<name>trik::TrikBrick</name>
134134
<message>
135-
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+82"/>
135+
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+84"/>
136136
<location line="+76"/>
137137
<source>2d model shell part was not found</source>
138138
<translation type="unfinished"></translation>

qrtranslations/ru/plugins/robots/trikKitInterpreterCommon_ru.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
<context>
197197
<name>trik::TrikBrick</name>
198198
<message>
199-
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+82"/>
199+
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+84"/>
200200
<location line="+76"/>
201201
<source>2d model shell part was not found</source>
202202
<translation>Консоль 2d модели не найдена</translation>

0 commit comments

Comments
 (0)