将QMutex添加到类中后,“尝试引用已删除的函数”

时间:2018-06-27 00:55:01

标签: c++ qt qt5 qmutex

我正在使用Qt5构建应用程序。我的程序构建并运行良好,但是访问数据结构的两个线程之间存在冲突。我有一个CanMessage对象的QList,我想使用QMutex保护其中的一些数据。但是,一旦我将QMutex添加到类定义中,就会收到错误消息:

QList.h: `error: C2280: 
'CanMessage::CanMessage(const CanMessage &)': attempting to reference a deleted function`. 

这是我的canmessage.h文件:

#ifndef CANMESSAGE_H
#define CANMESSAGE_H

#include <QObject>
#include <QMutex>
#include "cansignal.h"

class CanMessage
{
public:
    CanMessage();
    /* snip - public function prototypes */

private:
    /* snip - private prototypes and data */
    QMutex m_messageMutex;
};

#endif // CANMESSAGE_H

还有cansignal.h

#ifndef CANSIGNAL_H
#define CANSIGNAL_H

#include <QObject>
#include <QDebug>
#include <QByteArray>

class CanSignal
{
public:
    CanSignal();

    CanSignal(QString &signalName, quint8 &signalLength, quint8 &signalStartBit,
                   float &offset, float &factor, bool &isBigEndian, bool &isFloat, bool &isSigned)
    {
        this->m_name = signalName;
        this->m_length = signalLength;
        this->m_startBit = signalStartBit;
        this->m_offset = offset;
        this->m_factor = factor;
        this->m_isBigEndian = isBigEndian;
        this->m_isFloat = isFloat;
        this->m_isSigned = isSigned;
    }

    bool setName(QString &signalName);
    bool setBitLength(quint8 &length);
    bool setStartBit(quint8 &startBit);
    bool setOffset(float &offset);
    bool setFactor(float &factor);
    bool setEndianess(bool &isBigEndian);
    bool setIsFloat(bool &isFloat);
    bool setIsSigned(bool &isSigned);
    void setValid();
    void setInvalid();
    void setEngineeringData(float data);

    QString getName();
    quint8 getBitLength();
    quint8 getStartBit();
    float getOffset();
    float getFactor();
    float getData();
    bool isBigEndian();
    bool isFloat();
    bool isSigned();
    bool getSignalValidity();


private:
    QString m_name;
    quint8 m_length;
    quint8 m_startBit;
    float m_offset;
    float m_factor;
    float m_data_float = 0;
    bool m_isBigEndian;
    bool m_isFloat;
    bool m_isSigned;
    // Set After everything in signal is filled
    bool m_isSignalValid = false;
};

#endif // CANSIGNAL_H

1 个答案:

答案 0 :(得分:1)

CanMessage::CanMessage(const CanMessage &)

是复制构造函数,显然用于将项目放置到列表中。由于QMutex不能实际上是可复制的,因此这行不通。

如何解决它取决于许多事情。也许最简单的方法是修改CanMessage,使其具有动态的互斥锁(当然是在构造函数中创建的)。

然后有一个复制构造函数,该构造函数首先锁定 source 对象互斥量,然后在目标对象中动态分配 new 互斥量。

这样,您可以确保复制时旧对象是“干净的”(因为您有其互斥锁),并且不会存在“试图复制不可复制的成员”的问题,因为互斥锁本身不是 复制。有关详细信息,请参见脚注(a)


下面的代码是显示问题的完整完整代码段,只要您将QMutex m_mutex;行注释掉即可,编译正常:

#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
    int m_xyzzy;
    //QMutex m_mutex;
public:
    Xyzzy() : m_xyzzy(0) {};
    Xyzzy(int val) : m_xyzzy(val) {};
};

int main() {
    QList<Xyzzy> myList;
    Xyzzy plugh;
    myList.push_back(plugh);
    return 0;
}

一旦您取消注释这一行,编译器便会正确地抱怨:

 error: use of deleted function 'Xyzzy::Xyzzy(const Xyzzy&)'

(a)在解决问题方面,您可以执行以下操作:

#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
    int m_xyzzy;
    QMutex *m_mutex; // Now a pointer
public:
    Xyzzy() : m_xyzzy(0) {
        m_mutex = new QMutex(); // Need to create in constructor.
        std::cout << "constructor " << m_mutex << '\n';
    };
    ~Xyzzy() {
        std::cout << "destructor " << m_mutex << '\n';
        delete m_mutex; // Need to delete in destructor.
    }
    Xyzzy(const Xyzzy &old) {
        old.m_mutex->lock();
        m_mutex = new QMutex(); // Need to make new one here.
        std::cout << "copy constructor from " << old.m_mutex
                  << " to " << m_mutex << '\n';
        old.m_mutex->unlock();
    }
};

int main() {
    QList<Xyzzy> myList;
    Xyzzy plugh;
    myList.push_back(plugh);
    return 0;
}

根据以下测试运行,该程序可以正常工作:

constructor 0x21c9e50
copy constructor from 0x21c9e50 to 0x21c9ec0
destructor 0x21c9e50
destructor 0x21c9ec0

在实际代码中,我可能会选择智能指针,而不是原始的new/delete调用,但这仅是为了说明概念。此外,您还需要处理所有其他可能性,这些可能性将按照现有的3/5 / whatever-comes-next的规则从现有对象创建一个新对象,当前(来自内存)仅限于副本分配成员{{ }}。