[simulation] Fix some potential crashes & 26 memory leaks, improve LCD redraws. (#4634)

* [simulation] Hardening: Fix some potential crashes & 26 memory leaks, improve LCD performance.
  * [simpgmspace] Init trims; Make sure `REa` is really defined (to match board files).
  * [simpgmspace][opentxsimulator] Verify current running state before start/stop; Move rotary enc. init.
  * [simpgmspace][LcdWidget] Improve performance by moving LCD content change check to lcdRefresh() & limiting LcdWidget refresh time to 60 fps max.
  * [simueeprom] Ensure thread could be started, set default running state to false.
  * [simufatfs] Fix paths report trace.
  * [eepromimportexport] Fix memory leaks resulting from import debugging scheme being used.
  * [customdebug] Introduce new scheme for custom debug output in compliance with Qt recommendations (see docs for QLoggingCategory).
  * [opentxeeprom] Fix extra conversion table cache elements being created and also not properly deleted (and hence leaking).
  * [opentxinterface] Unregister EEpromInterfaces in unregisterOpenTxFirmwares();
  * [storage] Unregister storage factories on exit (fixes leak); create virtual StorageFormat/StorageFactory destructors (prevents warnings).
  * [helpers] GVarGroup now emits own signal, no need to pass ModelPanel pointer (removes dependency on modeledit.h)
  * [DebugOutput] Clear simulator trace hook before exiting (prevent possible issues); Fix leak and possible bad QString allocations when reading from buffer; Fix leak with combo box event filter.
  * [TelemetrySimulator] Fix leak by deleting LogPlaybackController object on exit; Only set up data fields once; convert timers to static.
  * [build] Consolidate all Companion/Simulator shared items in `common` library to reduce build time/etc (node & edge still remain awkward).

* [simulatorwidget] Delete removed spacer object (previously-forgotten "26th" leak).

* [DebugOutput] Fix stray trailing characters issue with new text buffer allocation (from previous commit); Increase maximum buffer sizes to better accommodate slower systems.

* Cosmetics.
This commit is contained in:
Max Paperno
2017-03-21 07:43:40 +01:00
committed by Bertrand Songis
parent 52e65179a8
commit 4aa0c1bbe4
28 changed files with 290 additions and 210 deletions
+8 -4
View File
@@ -71,7 +71,7 @@ DebugOutput::DebugOutput(QWidget * parent, SimulatorInterface *simulator):
ui->filterText->addItem(fltr, "no_delete");
ui->filterText->setValidator(new DebugOutputFilterValidator(ui->filterText));
ui->filterText->installEventFilter(new DeleteComboBoxItemEventFilter());
ui->filterText->installEventFilter(new DeleteComboBoxItemEventFilter(this));
ui->actionShowFilterHelp->setIcon(SimulatorIcon("info"));
ui->actionWordWrap->setIcon(SimulatorIcon("word_wrap"));
@@ -115,6 +115,7 @@ DebugOutput::DebugOutput(QWidget * parent, SimulatorInterface *simulator):
DebugOutput::~DebugOutput()
{
m_simulator->installTraceHook(NULL);
saveState();
if (AppDebugMessageHandler::instance())
@@ -173,15 +174,18 @@ void DebugOutput::restoreState()
void DebugOutput::processBytesReceived()
{
static char buf[512];
const QTextCursor savedCursor(ui->console->textCursor());
const int sbValue = ui->console->verticalScrollBar()->value();
const bool sbAtBottom = (sbValue == ui->console->verticalScrollBar()->maximum());
qint64 len;
QString text;
while ((len = m_dataBufferDevice->bytesAvailable()) > 0) {
QString text(m_dataBufferDevice->read(qMin(len, qint64(512))));
if (text.isEmpty())
while (m_dataBufferDevice && m_dataBufferDevice->bytesAvailable() > 0) {
len = m_dataBufferDevice->read(buf, sizeof(buf));
if (len <= 0)
break;
text = QString::fromLocal8Bit(buf, len);
ui->console->moveCursor(QTextCursor::End);
ui->console->textCursor().insertText(text);
if (sbAtBottom) {
+6 -3
View File
@@ -29,14 +29,15 @@
#include <QValidator>
#include <QWidget>
// NOTE : The buffer sizes need to be large enough to handle the flood of data when X12/X10 simulator starts up (almost 40K!).
// NOTE : The buffer sizes need to be large enough to handle the flood of data when X12/X10 simulator starts up with TRACE_SIMPGMSPACE=1 (> 40K!).
// These are maximum sizes, not necessarily allocated sizes.
#ifndef DEBUG_OUTPUT_WIDGET_OUT_BUFF_SIZE
// This buffer holds received and processed data until it can be printed to our console.
#define DEBUG_OUTPUT_WIDGET_OUT_BUFF_SIZE (40 * 1024) // [bytes]
#define DEBUG_OUTPUT_WIDGET_OUT_BUFF_SIZE (50 * 1024) // [bytes]
#endif
#ifndef DEBUG_OUTPUT_WIDGET_INP_BUFF_SIZE
// This buffer is active if line filter is enabled and holds received data until it can be filtered and placed in output buffer.
#define DEBUG_OUTPUT_WIDGET_INP_BUFF_SIZE (30 * 1024) // [bytes]
#define DEBUG_OUTPUT_WIDGET_INP_BUFF_SIZE (50 * 1024) // [bytes]
#endif
namespace Ui {
@@ -101,6 +102,8 @@ class DebugOutputFilterValidator : public QValidator
class DeleteComboBoxItemEventFilter : public QObject
{
Q_OBJECT
public:
DeleteComboBoxItemEventFilter(QObject *parent = Q_NULLPTR) : QObject(parent) { }
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
@@ -33,6 +33,8 @@
#endif
#include <QDebug>
#include <QLabel>
#include <QMessageBox>
extern AppData g; // ensure what "g" means
@@ -38,6 +38,7 @@
#endif
#include <QFile>
#include <QMessageBox>
#include <iostream>
SimulatorWidget::SimulatorWidget(QWidget * parent, SimulatorInterface *simulator, quint8 flags):
@@ -99,6 +100,7 @@ SimulatorWidget::SimulatorWidget(QWidget * parent, SimulatorInterface *simulator
keymapHelp.append(item);
ui->radioUiWidget->layout()->removeItem(ui->radioUiTempSpacer);
delete ui->radioUiTempSpacer;
ui->radioUiWidget->layout()->addWidget(radioUiWidget);
radioUiWidget->setFocusPolicy(Qt::WheelFocus);
radioUiWidget->setFocus();
@@ -26,6 +26,7 @@
#include "radiodata.h"
#include "simulator.h"
#include <QTimer>
#include <QWidget>
#include <QVector>
+21 -27
View File
@@ -31,12 +31,12 @@ TelemetrySimulator::TelemetrySimulator(QWidget * parent, SimulatorInterface * si
{
ui->setupUi(this);
timer = new QTimer(this);
timer->setInterval(10);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimerEvent()));
setupDataFields();
logTimer = new QTimer(this);
connect(logTimer, SIGNAL(timeout()), this, SLOT(onLogTimerEvent()));
timer.setInterval(10);
connect(&timer, &QTimer::timeout, this, &TelemetrySimulator::generateTelemetryFrame);
connect(&logTimer, &QTimer::timeout, this, &TelemetrySimulator::onLogTimerEvent);
connect(ui->Simulate, SIGNAL(toggled(bool)), this, SLOT(onSimulateToggled(bool)));
connect(ui->loadLogFile, SIGNAL(released()), this, SLOT(onLoadLogFile()));
@@ -64,33 +64,27 @@ TelemetrySimulator::TelemetrySimulator(QWidget * parent, SimulatorInterface * si
TelemetrySimulator::~TelemetrySimulator()
{
timer->stop();
logTimer->stop();
timer.stop();
logTimer.stop();
delete logPlayback;
delete ui;
}
void TelemetrySimulator::onSimulateToggled(bool isChecked)
{
if (isChecked) {
timer->start();
timer.start();
}
else {
timer->stop();
timer.stop();
}
}
void TelemetrySimulator::onLogTimerEvent()
{
logPlayback->stepForward(false);
}
void TelemetrySimulator::onTimerEvent()
{
generateTelemetryFrame();
}
void TelemetrySimulator::onLoadLogFile()
{
onStop(); // in case we are in playback mode
@@ -100,7 +94,7 @@ void TelemetrySimulator::onLoadLogFile()
void TelemetrySimulator::onPlay()
{
if (logPlayback->isReady()) {
logTimer->start(logPlayback->logFrequency * 1000 / SPEEDS[ui->replayRate->value()]);
logTimer.start(logPlayback->logFrequency * 1000 / SPEEDS[ui->replayRate->value()]);
logPlayback->play();
}
}
@@ -108,7 +102,7 @@ void TelemetrySimulator::onPlay()
void TelemetrySimulator::onRewind()
{
if (logPlayback->isReady()) {
logTimer->stop();
logTimer.stop();
logPlayback->rewind();
}
}
@@ -116,7 +110,7 @@ void TelemetrySimulator::onRewind()
void TelemetrySimulator::onStepForward()
{
if (logPlayback->isReady()) {
logTimer->stop();
logTimer.stop();
logPlayback->stepForward(true);
}
}
@@ -124,7 +118,7 @@ void TelemetrySimulator::onStepForward()
void TelemetrySimulator::onStepBack()
{
if (logPlayback->isReady()) {
logTimer->stop();
logTimer.stop();
logPlayback->stepBack();
}
}
@@ -132,7 +126,7 @@ void TelemetrySimulator::onStepBack()
void TelemetrySimulator::onStop()
{
if (logPlayback->isReady()) {
logTimer->stop();
logTimer.stop();
logPlayback->stop();
}
}
@@ -147,21 +141,21 @@ void TelemetrySimulator::onPositionIndicatorChanged(int value)
void TelemetrySimulator::onReplayRateChanged(int value)
{
if (logTimer->isActive()) {
logTimer->setInterval(logPlayback->logFrequency * 1000 / SPEEDS[ui->replayRate->value()]);
if (logTimer.isActive()) {
logTimer.setInterval(logPlayback->logFrequency * 1000 / SPEEDS[ui->replayRate->value()]);
}
}
void TelemetrySimulator::closeEvent(QCloseEvent *event)
{
timer->stop();
logTimer->stop();
timer.stop();
logTimer.stop();
event->accept();
}
#define SET_INSTANCE(control, id, def) ui->control->setText(QString::number(simulator->getSensorInstance(id, ((def) & 0x1F) + 1)))
void TelemetrySimulator::showEvent(QShowEvent *event)
void TelemetrySimulator::setupDataFields()
{
SET_INSTANCE(rxbt_inst, BATT_ID, 0);
SET_INSTANCE(rssi_inst, RSSI_ID, 24);
@@ -410,7 +404,7 @@ void TelemetrySimulator::generateTelemetryFrame()
if (ok && (buffer[2] || buffer[3]))
simulator->sendTelemetry(buffer, FRSKY_SPORT_PACKET_SIZE);
else
onTimerEvent();
generateTelemetryFrame();
}
uint32_t TelemetrySimulator::FlvssEmulator::encodeCellPair(uint8_t cellNum, uint8_t firstCellNo, double cell1, double cell2)
+4 -5
View File
@@ -49,9 +49,9 @@ class TelemetrySimulator : public QWidget
protected slots:
virtual void closeEvent(QCloseEvent *event);
virtual void showEvent(QShowEvent *event);
void setupDataFields();
void onSimulateToggled(bool isChecked);
void onTimerEvent();
void generateTelemetryFrame();
void onLogTimerEvent();
void onLoadLogFile();
void onPlay();
@@ -65,10 +65,9 @@ class TelemetrySimulator : public QWidget
protected:
Ui::TelemetrySimulator * ui;
QTimer * timer;
QTimer * logTimer;
QTimer timer;
QTimer logTimer;
SimulatorInterface *simulator;
void generateTelemetryFrame();
// protected classes follow
+24 -14
View File
@@ -26,10 +26,15 @@
#include <QPainter>
#include <QClipboard>
#include <QDir>
#include <QElapsedTimer>
#include <QMutex>
#include <QMutexLocker>
#include "appdata.h"
#include "appdebugmessagehandler.h"
#define LCD_WIDGET_REFRESH_PERIOD 16 // [ms] 16 = 62.5fps
class LcdWidget : public QWidget
{
Q_OBJECT
@@ -39,7 +44,7 @@ class LcdWidget : public QWidget
LcdWidget(QWidget * parent = 0):
QWidget(parent),
lcdBuf(NULL),
previousBuf(NULL),
localBuf(NULL),
lightEnable(false),
bgDefaultColor(QColor(198, 208, 199))
{
@@ -47,8 +52,8 @@ class LcdWidget : public QWidget
~LcdWidget()
{
if (previousBuf) {
free(previousBuf);
if (localBuf) {
free(localBuf);
}
}
@@ -62,8 +67,9 @@ class LcdWidget : public QWidget
lcdSize = (width * height) * ((depth+7) / 8);
else
lcdSize = (width * ((height+7)/8)) * depth;
previousBuf = (unsigned char *)malloc(lcdSize);
memset(previousBuf, 0, lcdSize);
localBuf = (unsigned char *)malloc(lcdSize);
memset(localBuf, 0, lcdSize);
}
void setBgDefaultColor(const QColor & color)
@@ -102,10 +108,12 @@ class LcdWidget : public QWidget
void onLcdChanged(bool light)
{
if (light != lightEnable || memcmp(previousBuf, lcdBuf, lcdSize)) {
lightEnable = light;
memcpy(previousBuf, lcdBuf, lcdSize);
QMutexLocker locker(&lcdMtx);
lightEnable = light;
memcpy(localBuf, lcdBuf, lcdSize);
if (!redrawTimer.isValid() || redrawTimer.hasExpired(LCD_WIDGET_REFRESH_PERIOD)) {
update();
redrawTimer.start();
}
}
@@ -117,24 +125,26 @@ class LcdWidget : public QWidget
int lcdSize;
unsigned char *lcdBuf;
unsigned char *previousBuf;
unsigned char *localBuf;
bool lightEnable;
QColor bgColor;
QColor bgDefaultColor;
QMutex lcdMtx;
QElapsedTimer redrawTimer;
inline void doPaint(QPainter & p)
{
QRgb rgb;
uint16_t z;
if (!lcdBuf)
if (!localBuf)
return;
if (lcdDepth == 16) {
for (int x = 0; x < lcdWidth; x++) {
for (int y = 0; y < lcdHeight; y++) {
z = ((uint16_t *)lcdBuf)[y * lcdWidth + x];
z = ((uint16_t *)localBuf)[y * lcdWidth + x];
rgb = qRgb(255 * ((z & 0xF800) >> 11) / 0x1F,
255 * ((z & 0x07E0) >> 5) / 0x3F,
255 * (z & 0x001F) / 0x1F);
@@ -147,7 +157,7 @@ class LcdWidget : public QWidget
if (lcdDepth == 12) {
for (int x = 0; x < lcdWidth; x++) {
for (int y = 0; y < lcdHeight; y++) {
z = ((uint16_t *)lcdBuf)[y * lcdWidth + x];
z = ((uint16_t *)localBuf)[y * lcdWidth + x];
rgb = qRgb(255 * ((z & 0xF00) >> 8) / 0x0F,
255 * ((z & 0x0F0) >> 4) / 0x0F,
255 * (z & 0x00F) / 0x0F);
@@ -181,12 +191,12 @@ class LcdWidget : public QWidget
mask = (1 << (y % 8));
for (int x = 0; x < lcdWidth; x++, idx++) {
if (lcdDepth == 1) {
if (lcdBuf[idx] & mask)
if (localBuf[idx] & mask)
p.drawRect(2 * x, 2 * y, 1, 1);
continue;
}
// lcdDepth == 4
z = (y & 1) ? (lcdBuf[idx] >> 4) : (lcdBuf[idx] & 0x0F);
z = (y & 1) ? (localBuf[idx] >> 4) : (localBuf[idx] & 0x0F);
if (!z)
continue;
if (z != previousDepth) {