Qt类型转换和内存分配释放遇到的坑

**1.类型转换,想把long long 变量转化为unsigned char ***
错误代码1

 long long  ptrImgBuffer;
 QString str = QString::number(ptrImgBuffer);
 QByteArray qStr = str.toLatin1();
 unsigned char *ptrAdress;
 ptrAdress = reinterpret_cast<unsigned char*>(qStr.data());

错误代码2,不小给删了

在这里插入代码片

正确代码,直接强制转换

unsigned char* yyy;
yyy = (unsigned char*)ptrImgBuffer;

转换错误,导致地址传递错误,转换正确才能保证地址传递正确
2.动态分配内存和释放内存导致的错误
1)分配内存,分配内存和内存拷贝,没有错,之前一直以为拷贝内存时有错

       pDlg->m_pImageBuffer = new unsigned char[imgWidth*imgHeight];

       if (pDlg->m_pImageBuffer == NULL)
          {
           qDebug()<<QString::fromLocal8Bit("分配内存空间不成功!");
           return ;
          }
       unsigned char* yyy;
       yyy = (unsigned char*)ptrImgBuffer;//获取图像地址

       memcpy( pDlg->m_pImageBuffer, yyy, imgWidth*imgHeight);//拷贝图像内存

2)QImage显示图像

      QImage image8=QImage( pDlg->m_pImageBuffer,imgWidth,imgHeight,QImage::Format_Indexed8);
      //QImage image8=QImage(yyy,imgWidth,imgHeight,QImage::Format_Indexed8);//实验
      //image8.setColorTable(pDlg->colorTable);//不需要
      //QImage image8=QImage(yyy,imgWidth,imgHeight,QImage::Format_Mono);//不行
      //QImage image8=QImage(yyy,imgWith,imgHeight,QImage::Format_MonoLSB);//不行
       pDlg->imageQueqe.enqueue(image8);//将图像存入缓冲队列

将图像缓冲区的首地址,和区域大小,分配给QImage,一直以为和QImage的函数有关,其实使用是正常的
3)释放内存,真正出错的地方在这里,存图完成后,我直接把内存释放了,所以导致我后面从队列中取出的image8存储的图像数据已经被删了,所以在发出信号 emit pDlg->getPicFinished();进行图像取用时直接崩掉

  if (pDlg->m_pImageBuffer != NULL)
       {
           delete[]pDlg->m_pImageBuffer;
           pDlg->m_pImageBuffer = NULL;
       }
       emit pDlg->getPicFinished();

4)取用图像时直接崩掉,因为缓冲区已经释放了

		if(!igrabthread->imageQueqe.empty())
        {
            imageQueue.enqueue(igrabthread->imageQueqe.dequeue());
        }
        if(!imageQueue.empty())
        {
            img=imageQueue.dequeue();

        }
        else
        {
            qDebug()<<QString::fromLocal8Bit("未能解析图像!:");
            return;
        }
        QPixmap pixmap=QPixmap::fromImage(img);
        ui->showCameralabel->setPixmap(pixmap);

5)解决办法,调整图像释放位置,或者向队列中存储非释放掉的缓冲区图像,或者加入缓冲队列
办法1,存储非释放掉的缓冲区图像,错误,可能时浅拷贝导致的

	QImage image8=QImage( pDlg->m_pImageBuffer,imgWidth,imgHeight,QImage::Format_Indexed8);
    QImage image=image8;
    pDlg->imageQueqe.enqueue(image);//存入缓冲队列

方法2,调整缓冲区位置,在显示完成后释放或者在关闭时释放,但是还不够,因为加入到队列中的image的图像都是指向了同一图像缓冲区

在这里插入代码片

方法3,不改变删除缓冲的位置,使用将QImage转化为QPixmap

	QImage image8=QImage( pDlg->m_pImageBuffer,imgWidth,imgHeight,QImage::Format_Indexed8);
    pDlg->pixmapQueqe.enqueue(QPixmap::fromImage(image8));//存入缓冲队列
```cpp
在这里插入代码片

浅拷贝就比如像引用类型,而深拷贝就比如值类型,即浅拷贝是共用一块内存的,而深拷贝是复制一份内容。
来看看QImage类的几个构造函数:
// 浅拷贝
QImage(uchar * data, int width, int height, Format format)
// 浅拷贝
QImage(const uchar * data, int width, int height, Format format)
// 浅拷贝
QImage(uchar * data, int width, int height, int bytesPerLine, Format format)
// 浅拷贝
QImage(const uchar * data, int width, int height, int bytesPerLine, Format format)
// 深拷贝
QImage QImage::copy(const QRect & rectangle = QRect()) const
// 浅拷贝
QImage QImage::rgbSwapped() const



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值