【QML】用 QQuickImageProvider 注意析构问题

文章讲述了在Qt环境中使用QQuickImageProvider时,通过engine.addImageProvider添加的裸指针不应在类析构函数中析构,因为Qt内部已使用智能指针进行管理。错误地双重析构会导致程序崩溃,特别是在使用单例模式时。代码示例展示了问题发生的具体场景以及错误析构导致的Segmentationfault错误。
  1. 结论:通过 engine.addImageProvider 添加的裸指针,在其所在类的析构函数中不要对其析构。因为在 addImageProvider() 的实现里面已经用了一个智能指针对其进行管理。如果在其所在类的析构函数中再次对其进行析构,则程序退出时将会崩溃。直接看代码。
// 报错信息如下:
The inferior stopped because it received a signal from the operating system.

Signal name :       SIGSEGV
Signal meaning :    Segmentation fault

// Debug 调试模式时,报错位置如下:
void QHashData::free_helper(void (*node_delete)(Node *))
{
    if (node_delete) {
        Node *this_e = reinterpret_cast<Node *>(this);
        Node **bucket = reinterpret_cast<Node **>(this->buckets);

        int n = numBuckets;
        while (n--) {
            Node *cur = *bucket++;
            while (cur != this_e) {
                Node *next = cur->next;
                node_delete(cur);                   // 指向这行代码!
                freeNode(cur);
                cur = next;
            }
        }
    }
    delete [] buckets;
    delete this;
}
  1. 代码:
// testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QDebug>
#include <QQuickImageProvider>

#define SINGLETON(x)     \
private: \
    x(x const&); \
    void operator=(x const&); \
public: \
static x* getInstance(QObject *parent = 0) \
{
     
      \
   static bool first=true; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值