简体   繁体   English

如何删除QSharedPointer

[英]How can I delete a QSharedPointer

I have written a wrapper around QTextEdit to use it like a QIODevice. 我围绕QTextEdit编写了一个包装器,以像QIODevice一样使用它。 I want to be able to use multiple wrapper with the same QTextEdit, so I may use different text color with each wrapper. 我希望能够对同一个QTextEdit使用多个包装,所以我可能对每个包装使用不同的文本颜色。

To make this wrapper thread-safe I added a QMutex to protect the usage of the QTextEdit. 为了使该包装器具有线程安全性,我添加了一个QMutex来保护QTextEdit的使用。 But I was thinking that I must use only one mutex to protect one QTextEdit. 但是我当时在考虑只使用一个互斥体来保护一个QTextEdit。

I end up with the following implementation, using a QSharedPointer to protect QTextEdit. 我最终使用QSharedPointer保护QTextEdit,实现了以下实现。

texteditiodevice.h texteditiodevice.h

class TextEditIODevice : public QIODevice
{
    Q_OBJECT

public:
    TextEditIODevice(QTextEdit * qTextEdit, QColor color, QObject * parent);

    virtual ~TextEditIODevice();

protected:
    qint64 readData(char *data, qint64 maxlen);

    qint64 writeData(const char *data, qint64 len);

private:
    /**
     * @brief Pointer to QTextEdit
     */
    QPointer<QTextEdit> textEdit;

    /**
     * @brief Text color
     */
    QColor color;

    /**
     * @brief Shared pointer to QTextEdit associated mutex
     */
    QSharedPointer<QMutex> mutex;

    /**
     * @brief Storage for QTextEdit associated mutexes
     */
    static QMap<QPointer<QTextEdit>, QSharedPointer<QMutex>> mutexes;
};

texteditiodevice.cpp texteditiodevice.cpp

QMap<QPointer<QTextEdit>, QSharedPointer<QMutex>> TextEditIODevice::mutexes;

TextEditIODevice::TextEditIODevice(QTextEdit * qTextEdit, QColor color, QObject * parent) :
    QIODevice(parent),
    textEdit(qTextEdit),
    color(color)
{
    open(QIODevice::WriteOnly | QIODevice::Text);

    qRegisterMetaType<QTextCharFormat>("QTextCharFormat");
    qRegisterMetaType<QTextBlock>("QTextBlock");
    qRegisterMetaType<QTextCursor>("QTextCursor");

    if(mutexes.contains(textEdit))
        mutex = mutexes[textEdit];
    else
    {
        mutex = QSharedPointer<QMutex>(new QMutex());
        mutexes.insert(textEdit, mutex);
    }
}

TextEditIODevice::~TextEditIODevice()
{
}

qint64 TextEditIODevice::readData(char *data, qint64 maxlen)
{
    Q_UNUSED(data);
    Q_UNUSED(maxlen);
    return 0;
}



qint64 TextEditIODevice::writeData(const char *data, qint64 len)
{
    if(textEdit)
    {
        mutex->lock();
        const QColor lastColor = textEdit->textColor();
        textEdit->setTextColor(color);
        textEdit->append(QString(data));
        textEdit->setTextColor(lastColor);
        mutex->unlock();
    }

    return len;
}

I want to know where I can remove QSharedPointer instance from mutexes mapping so the QMutex will be deleted. 我想知道在哪里可以从mutexes映射中删除QSharedPointer实例,因此QMutex将被删除。

Thanks for your help 谢谢你的帮助

As long as the shared pointer is in static mutexes map, it will never be deallocated, and the lifetime of mutexes is the lifetime of the program. 只要共享指针在静态mutexes映射中,就永远不会释放它,并且mutexes的生存期就是程序的生存期。

If you want to actually delete a mutex, you have to remove it from the mutexes mapping. 如果要实际删除互斥锁,则必须将其从mutexes映射中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM