Qt Crash SIGSEGV Fault while dynamically adding widgets to scrollArea (ui->scrollArea->setWidget(...))
<p><strong>My Intention</strong><br></p>

<ul>
<li><p>I want to create and add a <code>QWidget</code> to <code>scrollArea</code>. <br> Then</p></li>
<li><p>create and set a <code>grid layout</code> for that <code>QWidget</code>. <br> </p></li>
<li>Then fill up that <code>grid layout</code> with <code>push buttons</code> each of which
have a text whose value comes from the input <code>int* array</code>.<br></li>
</ul>

<p><strong>Overview of My Code:</strong><br>
I have a <code>QDialog</code> application with a <code>UI form</code>. <br>
In my UI form I have added a <code>scrollArea</code> using <code>QtDesigner</code>.<br>
In my program when some event happens I call the following function:<br></p>

<pre><code>void View::setUpBatteryTypes(int* array,int size)
{
    QGridLayout* gl=new QGridLayout();
    QWidget *viewport = new QWidget;
    viewport-&gt;setLayout(gl);
    qDebug()&lt;&lt;"debug test 1";
    ui-&gt;scrollArea-&gt;setWidget(viewport); //crashes here in 2nd call of func
    qDebug()&lt;&lt;"debug test 2";

    QPushButton* pushButtons=new QPushButton[size];
    for(int i=0;i&lt;size;i++)
    {
        pushButtons[i].setText(QVariant(array[i]).toString());
        pushButtons[i].setMinimumSize(200,100);
        pushButtons[i].setMaximumSize(400,200);
        pushButtons[i].setStyleSheet("background-color: rgb(170,170,197);");
        gl-&gt;addWidget(&amp;pushButtons[i],(int)floor(i/3),i%3);

    }  
}
</code></pre>

<p><strong>Problem</strong><br>
My problem is that when I call the function <code>setUpBatteryTypes</code> for the first time in my app everything is ok and the function does what I intend to do. However, when the function is called for the second time in my app it crashes in the commented line in between the two qDebug() statements. </p>

<p><hr>
Here is my code<br>
<strong>dialog.h</strong><br></p>

<pre><code>#ifndef DIALOG_H
#define DIALOG_H

#include &lt;QDialog&gt;

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;
public:
    void setUpBatteryTypes(int*,int);
};

#endif // DIALOG_H
</code></pre>

<p><strong>dialog.cpp</strong><br></p>

<pre><code>#include "dialog.h"
#include "ui_dialog.h"
#include &lt;QGridLayout&gt;
#include &lt;QDebug&gt;
#include &lt;QPushButton&gt;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui-&gt;setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::setUpBatteryTypes(int* array,int size)
{

    QGridLayout* gl=new QGridLayout();
    QWidget *viewport = new QWidget;
    viewport-&gt;setLayout(gl );
    qDebug()&lt;&lt;"debug test 1";
    qDebug()&lt;&lt;ui-&gt;scrollArea-&gt;children();
    ui-&gt;scrollArea-&gt;setWidget(viewport);
    qDebug()&lt;&lt;ui-&gt;scrollArea-&gt;children();
    qDebug()&lt;&lt;"debug test 2";

    QPushButton* pushButtons=new QPushButton[size];

    for(int i=0;i&lt;size;i++)
    {
        pushButtons[i].setText(QVariant(array[i]).toString());
        pushButtons[i].setMinimumSize(200,100);
        pushButtons[i].setMaximumSize(400,200);
        pushButtons[i].setStyleSheet("background-color: rgb(170,170,197);");
        gl-&gt;addWidget(&amp;pushButtons[i],(int)floor(i/3),i%3);
    }
}
</code></pre>

<p><strong>main.cpp</strong><br></p>

<pre><code>#include "dialog.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog* w=new Dialog();
    w-&gt;show();
    int size=4;
    int* array=new int[size];
    for(int i=0;i&lt;size;i++)
    {
        array[i]=i;
    }

    w-&gt;setUpBatteryTypes(array,size);
    w-&gt;setUpBatteryTypes(array,size);

    return a.exec();
}
</code></pre>

<p><strong>dialog.ui</strong>   </p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;Dialog&lt;/class&gt;
 &lt;widget class="QDialog" name="Dialog"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;400&lt;/width&gt;
    &lt;height&gt;300&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;Dialog&lt;/string&gt;
  &lt;/property&gt;
  &lt;layout class="QVBoxLayout" name="verticalLayout"&gt;
   &lt;item&gt;
    &lt;widget class="QScrollArea" name="scrollArea"&gt;
     &lt;property name="widgetResizable"&gt;
      &lt;bool&gt;true&lt;/bool&gt;
     &lt;/property&gt;
     &lt;widget class="QWidget" name="scrollAreaWidgetContents"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;0&lt;/x&gt;
        &lt;y&gt;0&lt;/y&gt;
        &lt;width&gt;380&lt;/width&gt;
        &lt;height&gt;280&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/widget&gt;
   &lt;/item&gt;
  &lt;/layout&gt;
 &lt;/widget&gt;
 &lt;layoutdefault spacing="6" margin="11"/&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>

<p><br>
<hr>
I debugged my code and it shows a SIGSEGV fault.<br>I also updated the question title accordingly.  </p>