在构造函数中写入大小为4无效

时间:2016-02-14 15:26:41

标签: c++ qt memory-leaks valgrind

我正在使用Qt5。在我正在编写的程序中,我有一个带有非常简单的构造函数的类。问题是,当我检查Valgrind的问题时,我会在每个作业中得到“无效的写入大小”。

这是标题:

#ifndef RS22812_H
#define RS22812_H

#include <QObject>

class RS22812 : public QObject
{
    Q_OBJECT
public:
    explicit RS22812(QObject *parent = 0);
    ~RS22812();
private:
    uint mode;
    Flags flags;
    QString byte2Digit(uchar byte);

signals:
    void modeChanged(uint newmode);

public slots:
    void newValue(const QByteArray &data);
};

#endif // RS22812_H

cpp文件中的构造函数定义:

RS22812::RS22812(QObject *parent) : QObject(parent)
{
    mode=0;
}

Valgrind在QObject(父级)和mode = 0:

时都给出了错误
Invalid write of size 4
  in RS22812::RS22812(QObject*) in /home/javi/ProgrammingProjects/multimeterGUI/rs22812.cpp:4
  1: RS22812::RS22812(QObject*) in /home/javi/ProgrammingProjects/multimeterGUI/rs22812.cpp:4
  2: MainWindow::MainWindow(QWidget*) in /home/javi/ProgrammingProjects/multimeterGUI/mainwindow.cpp:29
  3: main in /home/javi/ProgrammingProjects/multimeterGUI/main.cpp:7
Address 0x1c4ad500 is 0 bytes after a block of size 16 alloc'd  1: operator new(unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/javi/ProgrammingProjects/multimeterGUI/mainwindow.cpp:29
  3: main in /home/javi/ProgrammingProjects/multimeterGUI/main.cpp:7

Invalid write of size 4
  in RS22812::RS22812(QObject*) in /home/javi/ProgrammingProjects/multimeterGUI/rs22812.cpp:6
  1: RS22812::RS22812(QObject*) in /home/javi/ProgrammingProjects/multimeterGUI/rs22812.cpp:6
  2: MainWindow::MainWindow(QWidget*) in /home/javi/ProgrammingProjects/multimeterGUI/mainwindow.cpp:29
  3: main in /home/javi/ProgrammingProjects/multimeterGUI/main.cpp:7
Address 0x1c4ad500 is 0 bytes after a block of size 16 alloc'd  1: operator new(unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  2: MainWindow::MainWindow(QWidget*) in /home/javi/ProgrammingProjects/multimeterGUI/mainwindow.cpp:29
  3: main in /home/javi/ProgrammingProjects/multimeterGUI/main.cpp:7

我不明白这么简单的任务怎么会导致错误。谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

尝试了几件事之后,似乎问题出现在QtCreator上。当我清理项目时,它编译没有问题。