Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. GlWidget in Plugin crash on destroy

GlWidget in Plugin crash on destroy

Scheduled Pinned Locked Moved General and Desktop
opengl plugin c
2 Posts 1 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CatBehemothC Offline
    CatBehemothC Offline
    CatBehemoth
    wrote on last edited by CatBehemoth
    #1

    Hi there,

    I recently implemented a simple opengl widget to display the 3D point cloud into the plugin. The widget works fine, but when I load the plugin and don't show it, only destroy at the end of application run i got a segmentation fault in the destructor of the plugin. The GlWidget class has been used in the form editor to subclass a widget object (but it happens when I create the window manually). The Plugin crashes in the area of a secondary display too.

    When I use the GlWidget class like a normal form the problem doesn't occur. Do I forget some initialization?

    Below is the opengl widget class and lik to simple Qt5.4 project where the problem occurs.

    here is the link to the sample project

    (header)
    *#ifndef GLWIDGET_H
    #define GLWIDGET_H

    #include <QGLWidget>
    #include <QGLShaderProgram>

    class GlWidget : public QGLWidget
    {
    Q_OBJECT

    public:
    explicit GlWidget(QWidget *parent = 0);
    ~GlWidget();
    QSize sizeHint() const;

    void addVertices(QVector<QVector3D> &vertices, QVector<QVector3D> colors);
    void setVertices(const QVector<QVector3D> &vertices, const QVector<QVector3D> &colors);
    void remVertices();
    

    protected:
    void initializeGL();
    void resizeGL(int width, int height);
    void paintGL();
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

    private:

    QMatrix4x4 pMatrix;
    QGLShaderProgram shaderProgram;
    QVector<QVector3D> m_vertices;
    QVector<QVector3D> m_colors;
    
    double alpha;
    double beta;
    double distance;
    QPoint lastMousePosition;
    
    QVector<QVector3D> m_grid;
    

    };

    #endif // GLWIDGET_H*

    (source)

    *#include "glwidget.h"
    #include <QMouseEvent>
    #include <QWheelEvent>

    GlWidget::GlWidget(QWidget parent)
    : QGLWidget(QGLFormat(/
    Additional format options */), parent)
    {
    alpha = 25;
    beta = -25;
    distance = 10.0;

    // define the grid
    m_grid << QVector3D(-50, 0,  0) << QVector3D( 50, 0,  0) // X
           << QVector3D(0, -50,  0) << QVector3D( 0, 50,  0)// Y
           << QVector3D(0, 0, -50) << QVector3D( 0, 0,  50);// Z
    

    }

    GlWidget::~GlWidget()
    {
    qDebug () << this;
    }

    QSize GlWidget::sizeHint() const
    {
    return QSize(640, 480);
    }

    void GlWidget::remVertices()
    {
    m_vertices.clear();
    m_colors.clear();

    updateGL();
    

    }

    void GlWidget::addVertices(QVector<QVector3D> &vertices, QVector<QVector3D> colors)
    {
    for (QVector<QVector3D>::iterator i=vertices.begin();i!=vertices.end();i++)
    {
    m_vertices.append(*i);
    }

    for (QVector<QVector3D>::iterator i=colors.begin();i!=colors.end();i++)
    {
        m_colors.append(*i);
    }
    
    updateGL();
    

    }

    void GlWidget::setVertices(const QVector<QVector3D> &vertices, const QVector<QVector3D> & colors)
    {
    m_vertices.clear();
    m_colors.clear();

    m_vertices  = vertices;
    m_colors = colors;
    
    updateGL();
    

    }

    //! [0]
    void GlWidget::initializeGL()
    {
    //! [0]
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    qglClearColor(QColor(Qt::gray));
    
    shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    
    shaderProgram.link();
    

    }
    //! [1]

    void GlWidget::resizeGL(int width, int height)
    {
    if (height == 0) {
    height = 1;
    }

    pMatrix.setToIdentity();
    pMatrix.perspective(60.0, (float) width / (float) height, 0.001, 1000);
    
    glViewport(0, 0, width, height);
    

    }

    //! [2]
    void GlWidget::paintGL()
    {
    //! [2]
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;
    
    QMatrix4x4 cameraTransformation;
    cameraTransformation.rotate(alpha, 0, 1, 0);
    cameraTransformation.rotate(beta, 1, 0, 0);
    
    QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
    
    vMatrix.lookAt(cameraPosition, QVector3D(0, 1, 0), cameraUpDirection);
    
    //! [3]
    shaderProgram.bind();
    
    shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    
    shaderProgram.setAttributeArray("vertex", m_grid.constData());
    shaderProgram.enableAttributeArray("vertex");
    
    glDrawArrays(GL_LINES, 0, m_grid.size());
    
    shaderProgram.disableAttributeArray("vertex");
    
    shaderProgram.setAttributeArray("vertex", m_vertices.constData());
    shaderProgram.enableAttributeArray("vertex");
    
    shaderProgram.setAttributeArray("color", m_colors.constData());
    shaderProgram.enableAttributeArray("color");
    
    glDrawArrays(GL_POINTS, 0, m_vertices.size());
    
    shaderProgram.disableAttributeArray("vertex");
    
    shaderProgram.disableAttributeArray("color");
    
    shaderProgram.release();
    

    }
    //! [3]

    void GlWidget::mousePressEvent(QMouseEvent *event)
    {
    lastMousePosition = event->pos();

    event->accept();
    

    }

    void GlWidget::mouseMoveEvent(QMouseEvent *event)
    {
    int deltaX = event->x() - lastMousePosition.x();
    int deltaY = event->y() - lastMousePosition.y();

    if (event->buttons() & Qt::LeftButton)
    {
        alpha -= deltaX;
        while (alpha < 0) {
            alpha += 360;
        }
        while (alpha >= 360) {
            alpha -= 360;
        }
    
        beta -= deltaY;
        if (beta < -90) {
            beta = -90;
        }
        if (beta > 90) {
            beta = 90;
        }
    
        updateGL();
    }
    
    lastMousePosition = event->pos();
    
    event->accept();
    

    }

    void GlWidget::wheelEvent(QWheelEvent *event)
    {
    int delta = event->delta();

    if (event->orientation() == Qt::Vertical) {
        if (delta < 0) {
            distance *= 1.1;
        } else if (delta > 0) {
            distance *= 0.9;
        }
    
        updateGL();
    }
    
    event->accept();
    

    }*

    And the crash occurs here:

        Function: QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data() const
    

    0xc074ec 55 push %ebp
    0xc074ed <+0x0001> 89 e5 mov %esp,%ebp
    0xc074ef <+0x0003> 83 ec 04 sub $0x4,%esp
    0xc074f2 <+0x0006> 89 4d fc mov %ecx,-0x4(%ebp)
    0xc074f5 <+0x0009> 8b 45 fc mov -0x4(%ebp),%eax
    *** 0xc074f8 <+0x000c> 8b 00 mov (%eax),%eax ***
    0xc074fa <+0x000e> c9 leave
    0xc074fb <+0x000f> c3 ret

    1 Reply Last reply
    0
    • CatBehemothC Offline
      CatBehemothC Offline
      CatBehemoth
      wrote on last edited by
      #2

      [SOLVED]

      My bad... I took an obsolete old class. O:)

      QGLWidget
      This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

      i should use
      QOpenGLWidget

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved