1. 当页面消息比较多时候,会报错

 

QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setCompositionMode: Painter not active
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::scale: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setCompositionMode: Painter not active
QPainter::end: Painter not active, aborted


QThread::start: Failed to create thread (Unknown error 0x00000022.)

 

==========> ExitInstance :gLayerInfo delete 113286984<==============Unload CSProxy from Client.exe...
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

2. 注意观察,总结什么情况下报错

一秒一条消息进来
10秒刷新一次,10分钟都没有异常
5秒刷新一次,10分钟都没有异常
2秒刷新一次,10分钟都没有异常===>13分钟崩溃
1秒刷新一次,6分钟后有问题

一秒四条消息进来
2秒刷新一次,第4分钟出现异常
5秒刷新一次,第4分钟出现异常
10秒刷新一次,第4分钟出现异常

 

3. 在不确定什么缘由导致时,注释整块、部分代码去排除出问题的代码

出问题的代码如下

    if(!soundName.isEmpty()){
        QMediaPlayer    *player = new QMediaPlayer();;//播放器
        QMediaPlaylist  *playlist = new QMediaPlaylist();//播放列表
        player->setPlaylist(playlist);

        if (player->state()!=QMediaPlayer::PlayingState){
            playlist->setCurrentIndex(0);
        }
        playlist->setPlaybackMode(QMediaPlaylist::Sequential); //循环模式

        QString audiosPath = QCoreApplication::applicationDirPath() + "/audios/";

        playlist->clear();
        //放3遍
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        player->play();
    }

 结合网上资料

class Q_CORE_EXPORT QInternal {
public:
    enum PaintDeviceFlags {
        UnknownDevice = 0x00,
        Widget        = 0x01,
        Pixmap        = 0x02,
        Image         = 0x03,
        Printer       = 0x04,
        Picture       = 0x05,
        Pbuffer       = 0x06,    // GL pbuffer
        FramebufferObject = 0x07, // GL framebuffer object
        CustomRaster  = 0x08,
        MacQuartz     = 0x09,
        PaintBuffer   = 0x0a,
        OpenGL        = 0x0b
    };

QPainter::begin: Paint device returned engine == 0, type: 3

UnknownDevice==>

每条消息会初始化QMediaPlayer,多个QMediaPlayer;播放会造成设备UnknownDevice;并且std::bad_alloc 表示程序分配内存异常。

 

 

 

4.解决方法

使用QMutex mutex,lock()、unlock()对上述代码块加锁,并对QMediaPlayer等复用。

static QMutex mutex;
static QMediaPlayer    *player = new QMediaPlayer();//播放器
static QMediaPlaylist  *playlist = new QMediaPlaylist();//播放列表


void XXX::playSound(QString soundName){
    if(!soundName.isEmpty()){
        mutex.lock();

        qDebug() << "【AlarmMessage::playSound】 start " << soundName;
        player->setPlaylist(playlist);

        if (player->state()!=QMediaPlayer::PlayingState){
            playlist->setCurrentIndex(0);
        }
        playlist->setPlaybackMode(QMediaPlaylist::Sequential); //循环模式

        QString audiosPath = QCoreApplication::applicationDirPath() + "/audios/";

        playlist->clear();
        //放3遍
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        playlist->addMedia(QUrl::fromLocalFile(audiosPath+soundName+".mp3"));
        player->play();

        qDebug() << "【AlarmMessage::playSound】 end " << soundName;

        mutex.unlock();
    }
}

 

posted on 2021-01-13 15:51  刘达人186  阅读(2010)  评论(0编辑  收藏  举报