关于使用QLIST的指针越界的问题

ly_118 2016-01-21 11:54:15
我定义一个简单的类,在类的成员函数中调用QSeiralPortInfo::availiable()返回QLIST<QSerialPortInfo>,然后成员函数结束时 报错。

[code=c++]
#ifndef SERIALPORTINFOTEST_H
#define SERIALPORTINFOTEST_H

#include <QtWidgets/QMainWindow>
#include "ui_serialportinfotest.h"
#include <QSerialPortInfo>
class SerialPortInfoTest : public QMainWindow
{
Q_OBJECT

public:
SerialPortInfoTest(QWidget *parent = 0);
~SerialPortInfoTest();
void doSerialPort();
private:
Ui::SerialPortInfoTestClass ui;

QStringList portNameList;
};

#endif // SERIALPORTINFOTEST_H
[/code]

#include "serialportinfotest.h"

SerialPortInfoTest::SerialPortInfoTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
doSerialPort();
}

SerialPortInfoTest::~SerialPortInfoTest()
{

}
void SerialPortInfoTest::doSerialPort()
{
QList<QSerialPortInfo> serialPortInfoList;
serialPortInfoList = QSerialPortInfo::availablePorts();


foreach(const QSerialPortInfo &info, serialPortInfoList)
{
portNameList.append(info.portName());
}
}//就是这个函数结束时报错。
单步调试 发现进入到这里 发出了报错
Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to)
{
if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic)
while(from != to) --to, delete reinterpret_cast<T*>(to->v);
else if (QTypeInfo<T>::isComplex)
while (from != to) --to, reinterpret_cast<T*>(to)->~T();
}


但是为什么会引起这个错误呢?
...全文
1350 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_31776303 2017-10-07
  • 打赏
  • 举报
回复 1
楼主怎么解决的,我也遇到这个问题了?
iambabao 2016-10-17
  • 打赏
  • 举报
回复 1
我也遇到了类似的问题!!! 很是不解 都是调用了QSerialPortInfo::availablePorts()函数 同一段代码在两个程序中一个就可以找到串口返回QList,而在另一个程序中就出现了访问失败。
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            portNumBox->addItem(info.portName());
            portMsgBrowser->append(tr("Serial Port Message:"));
            portMsgBrowser->append(info.portName());
            portMsgBrowser->append(info.description());
            portMsgBrowser->append(info.manufacturer());
            portMsgBrowser->append(info.serialNumber());
            serial.close();
        }
    }
错误信息是:c:\users\qt\work\qt\qtbase\src\corelib\tools\qscopedpointer.h:141: error: Exception at 0x6c26da1a, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance) 求解

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧