+ -
当前位置:首页 → 问答吧 → 内存问题

内存问题

时间:2011-11-17

来源:互联网

C/C++ code

void IButton::setBlack(QString & name)
{
     Q_D(QAbstractButton);
    if (image_!=NULL)
    {
        delete []image_;
        image_ = new QImage();
    }
    image_->load(name);
    QSize s = size();
    QImage *i2 =new QImage(image_->scaled(s));
    *pixmap_ = QPixmap::fromImage(*i2);
        [color=#FF0000]
        setIcon(QIcon(*pixmap_)); //问题地方 此处删除 没有任何问题
        [/color]
    setIconSize(s);
    delete []i2;
}


我代码是QT上面一个BUTTON加载一任意大小的图片并自适应button大小。在使用中我的button被按下和释放都需要更改一次图片。但使用过程中,内存会不断增加,我测试过为 上面注释的地方出现问题。

我跟踪源码
C/C++ code

void QAbstractButton::setIcon(const QIcon &icon)
{
    Q_D(QAbstractButton);
    [color=#FF0000]
    d->icon = icon;
    [/color]
    d->sizeHint = QSize();
    update();
    updateGeometry();
}


C/C++ code

QIcon &QIcon::operator=(const QIcon &other)
{
    if (other.d)
        other.d->ref.ref();
    if (d && !d->ref.deref())
        delete d;
    d = other.d;
    return *this;
}


d 为 QIconPrivate 类型 我实在找不出那边有问题了。现在上述代码只要我多响应几次,如果图片大一些内存被吃掉的非常明显。哪位来99我啊

作者: tt2com   发布时间: 2011-11-17

没人用过嘛,哪位可以救救我啊

作者: tt2com   发布时间: 2011-11-25

嗯 我道义上支持楼主 

作者: WO_YOU_XIE_SHANG_XIN   发布时间: 2011-11-29

delete d没有执行过吧

作者: WO_YOU_XIE_SHANG_XIN   发布时间: 2011-11-29