本周技术概要20231016-20231022与生活点滴

2023-10-25 17:06:08 浏览数 (2)

一、linux下Qt treeView内存泄漏

当把原来Qt程序由ubuntu 32位的程序迁移到ubuntu 64位时,测试程序发现了内存泄漏问题,经过排查,最终定位在Qt的treeView的setCurrentIndex调用上。到目前还没有排查出来,有遇到同样问题的同志可以一起交流下。程序测试demo在64位环境下有明显内存泄漏,但是同样程序在32位上运行正常,接下来可能要继续更换Qt的版本进行测试了。

目前推测就是Qt自身Widget库的问题。

测试环境:Linux ubuntu 4.15.0-142-generic #146~16.04.1-Ubuntu

测试程序如下:

头文件:

代码语言:javascript复制
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModelIndex>
#include <QTreeView>

QT_BEGIN_NAMESPACE
class QTreeView; //forward declarations
class QStandardItemModel;
class QStandardItem;
class QTimer;

QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT

private slots:
    void slot_valueIncrease();
    void slot_modelIndex(QModelIndex index);

private:
    QTreeView *treeView;

    QTreeView TreeView;

    QStandardItemModel *standardModel;
    QList<QStandardItem *> prepareRow(const QString &first) const;

    QTimer *timer_;
    int count{0};

    QStandardItem *root_;
    QStandardItem *first_;
    QStandardItem *second_;


public:
    MainWindow(QWidget *parent = nullptr);
};

#endif // MAINWINDOW_H

cpp文件:

代码语言:javascript复制
    root_ = new QStandardItem("root");
    first_ = new QStandardItem("first");
    second_ = new QStandardItem("second");

    first_->appendRow(second_);
    root_->appendRow(first_);
定时器溢出后:
    treeView->setCurrentIndex(first_->index());
    treeView->setCurrentIndex(second_->index());

最后使用top指令观察RES值,会有明显连续增加。程序运行效果:

二、linux下

linux下USB转232、转485、转422的硬件验证

RS232 DB9接头2脚和三脚短接,实现“环回”

RS485 是半双工,无法环回。

RS422 DB9接头,短接13 短接24。

引脚具体图片:

RS232

RS422

Linux脚本测试指令:

1、利用stty命令去掉默认的回显参数

stty -F /dev/ttyUSB0 -echo -onlcr

2、一个终端输入:

cat /dev/ttyUSB0

1、另一个终端输入

echo "hello" > /dev/ttyUSB0

效果如图:

0 人点赞