A bug ghost!



  • I just meet a bug ghost in my code.That means if I try to find what happen inside,the code runs well.But if I don't, bugs happen.
    The code is trying to draw a histogram of an image using OpenCV functions and show it in qml.Here is the code.

    C++ code

    "histogram.h"
    #include <QtQuick/QQuickPaintedItem>
    #include <QImage>
    #include <QPainter>

    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>

    using namespace cv;

    class Histogram : public QQuickPaintedItem{

    Q_OBJECT
    Q_PROPERTY(QList<int> hist READ hist WRITE setHist)
    

    public:

    Histogram(QQuickItem *parent = 0);
    ~Histogram();
    void paint(QPainter *painter);
    QList<int> hist()const { return m_hist; }
    void setHist(const QList<int> arg);
    

    private:

    QList<int> m_hist;
    QImage histPainter;
    

    };

    "histogram.cpp"

    #include "histogram.h"
    Histogram::Histogram(QQuickItem *parent):
    QQuickPaintedItem(parent)
    {
    }
    Histogram::~Histogram()
    {
    }
    void Histogram::paint(QPainter *painter)
    {

    if(histPainter.isNull()){
        qDebug() << "histPainter is NULL!";
        return;
    }
    painter->drawImage(QPoint(0,0), histPainter);
    

    }
    void Histogram::setHist(const QList<int> arg)
    {

    if(arg.length() <=1)
    {
        qDebug() << "no Hist from QML!";
        return;
    }
    m_hist = arg;
    Mat histImage = Mat::ones(200, 320, CV_8UC3)*255;
    histImage = Scalar::all(200);
    int histSize = 320;
    int binW = cvRound((double)histImage.cols/histSize);
    for( int i = 0; i < histSize; i++ )
        rectangle( histImage, Point(i*binW, histImage.rows),
                   Point((i+1)*binW, histImage.rows - m_hist.at(i)),
                   Scalar::all(0), -1, 8, 0 );
    histPainter  = QImage((unsigned char*)histImage.data,
            histImage.cols, histImage.rows,
            histImage.step,
            QImage::Format_RGB888);
    
    //try to find what happened......
    //histPainter.save("dumin.jpg");
    //histPainter.load("dumin.jpg");
    
    update();
    

    }

    QML code

        Histogram{
            id: histogram
            width: 320; height: 200
        }
    

    If I use" histPainter.save("dumin.jpg"); histPainter.load("dumin.jpg");",it can show the histogram well.But if I comment the code out, it just shows something strange and not stable,just like a ghost.

    How should I catch this bug ghost???
    Thank you for reading my question! :)


  • Moderators

    @Duino said:

    I just meet a bug ghost in my code.That means if I try to find what happen inside,the code runs well.But if I don't, bugs happen.

    It's not a ghost. It's a Heisenbug :)

    How should I catch this bug ghost???

    Simplify your program, do unit testing. and use a tool like Valgrind.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.