Hello!
I have got a class SmsGui which contains 2 forms generated by qtdesigner: mainWindow and loginWindow. Firstly when program runs, mainWidnow is created and shown. When user presses Ok on mainWindow, loginWindow is created and shown. And here are 2 situations: in first situation I do:
connect(loginWindow->cancelButton, SIGNAL(clicked()), loginWindow, SLOT(close()));
connect(loginWindow->cancelButton, SIGNAL(clicked()), loginWindow, SLOT(close()));
To copy to clipboard, switch view to plain text mode
And loginDialog closes normally. But in a secod situation I call loginDialog->close() manually.
connect(loginWindow->cancelButton, SIGNAL(clicked()), this, SLOT(kill()));
void SmsGui::kill()
{
loginDialog->close();
}
connect(loginWindow->cancelButton, SIGNAL(clicked()), this, SLOT(kill()));
void SmsGui::kill()
{
loginDialog->close();
}
To copy to clipboard, switch view to plain text mode
And this gives me a segfault. Why? I just call the same function but in 2 different ways, so why it gives me a segfault?
Backtrace:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1223948608 (LWP 25558)]
0x0804d9a4 in SmsGui::kill (this=0x8061670) at sms_gui.cpp:125
125 loginDialog->close();
(gdb) backtrace
#0 0x0804d9a4 in SmsGui::kill (this=0x8061670)
at sms_gui.cpp:125
#1 0x0804d995 in SmsGui::clickedLoginOk (this=0x8061670)
at sms_gui.cpp:120
#2 0x08050d14 in SmsGui::qt_metacall (this=0x8061670,
_c
=QMetaObject::InvokeMetaMethod, _id
=5, _a
=0xbfa59790
) at moc_sms_gui.cpp:83
#3 0xb746132c in QMetaObject::activate ()
from /usr/lib/qt4/libQtCore_debug.so.4
#4 0xb746157e in QMetaObject::activate ()
from /usr/lib/qt4/libQtCore_debug.so.4
#5 0xb7e011f6 in QAbstractButton::clicked ()
from /usr/lib/qt4/libQtGui_debug.so.4
#6 0xb7c66455 in QAbstractButtonPrivate::click ()
from /usr/lib/qt4/libQtGui_debug.so.4
#7 0xb7c67779 in QAbstractButton::mouseReleaseEvent ()
from /usr/lib/qt4/libQtGui_debug.so.4
#8 0xb7a3879d in QWidget::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#9 0xb7c6761b in QAbstractButton::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#10 0xb7ce0d52 in QPushButton::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#11 0xb79ecea7 in QApplicationPrivate::notify_helper ()
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1223948608 (LWP 25558)]
0x0804d9a4 in SmsGui::kill (this=0x8061670) at sms_gui.cpp:125
125 loginDialog->close();
(gdb) backtrace
#0 0x0804d9a4 in SmsGui::kill (this=0x8061670)
at sms_gui.cpp:125
#1 0x0804d995 in SmsGui::clickedLoginOk (this=0x8061670)
at sms_gui.cpp:120
#2 0x08050d14 in SmsGui::qt_metacall (this=0x8061670,
_c=QMetaObject::InvokeMetaMethod, _id=5, _a=0xbfa59790)
at moc_sms_gui.cpp:83
#3 0xb746132c in QMetaObject::activate ()
from /usr/lib/qt4/libQtCore_debug.so.4
#4 0xb746157e in QMetaObject::activate ()
from /usr/lib/qt4/libQtCore_debug.so.4
#5 0xb7e011f6 in QAbstractButton::clicked ()
from /usr/lib/qt4/libQtGui_debug.so.4
#6 0xb7c66455 in QAbstractButtonPrivate::click ()
from /usr/lib/qt4/libQtGui_debug.so.4
#7 0xb7c67779 in QAbstractButton::mouseReleaseEvent ()
from /usr/lib/qt4/libQtGui_debug.so.4
#8 0xb7a3879d in QWidget::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#9 0xb7c6761b in QAbstractButton::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#10 0xb7ce0d52 in QPushButton::event ()
from /usr/lib/qt4/libQtGui_debug.so.4
#11 0xb79ecea7 in QApplicationPrivate::notify_helper ()
To copy to clipboard, switch view to plain text mode
Header file:
#ifndef SMS_GUI_H
#define SMS_GUI_H
#include <QDialog>
#include "ui_main_window.h"
#include "ui_login_dialog.h"
#include "types.h"
{
Q_OBJECT
public:
QString username, password, number, message;
bool limitCheck;
unsigned short int port;
void destroy();
void showMainWindow();
void connectSlots();
void disconnectSlots();
void setMiastoPlusaParameters();
void showLoginDialog();
Ui_mainWindow *mainWindow;
Ui_loginDialog *loginWindow;
public slots:
void enableSendButtonText();
void enableSendButtonLine
(const QString &newText
);
void resetAll();
void currentIndexChanged(int index);
void clickedLoginOk();
void clickedSend();
void clickedLoginCancel();
void closeMainWindow();
void kill();
public:
SmsGui();
~SmsGui();
signals:
void readyToSend(network);
};
#ifndef SMS_GUI_H
#define SMS_GUI_H
#include <QDialog>
#include "ui_main_window.h"
#include "ui_login_dialog.h"
#include "types.h"
class SmsGui : public QObject
{
Q_OBJECT
public:
QString username, password, number, message;
bool limitCheck;
unsigned short int port;
void destroy();
void showMainWindow();
void connectSlots();
void disconnectSlots();
void setMiastoPlusaParameters();
void showLoginDialog();
Ui_mainWindow *mainWindow;
QDialog *mainDialog;
Ui_loginDialog *loginWindow;
QDialog *loginDialog;
public slots:
void enableSendButtonText();
void enableSendButtonLine(const QString &newText);
void resetAll();
void currentIndexChanged(int index);
void clickedLoginOk();
void clickedSend();
void clickedLoginCancel();
void closeMainWindow();
void kill();
public:
SmsGui();
~SmsGui();
signals:
void readyToSend(network);
};
To copy to clipboard, switch view to plain text mode
source:
#include "sms_gui.h"
SmsGui::SmsGui()
{
showMainWindow();
connectSlots();
}
SmsGui::~SmsGui()
{
closeMainWindow();
}
void SmsGui::showMainWindow()
{
mainWindow = new Ui_mainWindow;
mainWindow->setupUi(mainDialog);
mainDialog->show();
}
void SmsGui::closeMainWindow()
{
mainDialog->reject();
delete mainDialog;
mainDialog = NULL;
delete mainWindow;
mainWindow = NULL;
}
void SmsGui::connectSlots()
{
connect(mainWindow->okButton, SIGNAL(clicked()), this, SLOT(clickedSend()));
}
void SmsGui::clickedSend()
{
//mainDialog->close();
switch(mainWindow->comboBox->currentIndex())
{
case 0: setMiastoPlusaParameters(); return;
case 1: /*setPlusWWWParameters();*/ return;
default: return;
}
}
void SmsGui::showLoginDialog()
{
loginWindow = new Ui_loginDialog;
loginWindow->setupUi(loginDialog);
connect(loginWindow->okButton, SIGNAL(clicked()), this, SLOT(clickedLoginOk()));
connect(loginWindow->cancelButton, SIGNAL(clicked()), this, SLOT(clickedLoginCancel()));
loginDialog->show();
}
void SmsGui::clickedLoginOk()
{
kill();
}
void SmsGui::kill()
{
loginDialog->close();
}
void SmsGui::clickedLoginCancel()
{
loginDialog->close();
}
void SmsGui::setMiastoPlusaParameters()
{
showLoginDialog();
}
#include "sms_gui.h"
SmsGui::SmsGui()
{
showMainWindow();
connectSlots();
}
SmsGui::~SmsGui()
{
closeMainWindow();
}
void SmsGui::showMainWindow()
{
mainDialog = new QDialog;
mainWindow = new Ui_mainWindow;
mainWindow->setupUi(mainDialog);
mainDialog->show();
}
void SmsGui::closeMainWindow()
{
mainDialog->reject();
delete mainDialog;
mainDialog = NULL;
delete mainWindow;
mainWindow = NULL;
}
void SmsGui::connectSlots()
{
connect(mainWindow->okButton, SIGNAL(clicked()), this, SLOT(clickedSend()));
}
void SmsGui::clickedSend()
{
//mainDialog->close();
switch(mainWindow->comboBox->currentIndex())
{
case 0: setMiastoPlusaParameters(); return;
case 1: /*setPlusWWWParameters();*/ return;
default: return;
}
}
void SmsGui::showLoginDialog()
{
QDialog *loginDialog = new QDialog;
loginWindow = new Ui_loginDialog;
loginWindow->setupUi(loginDialog);
connect(loginWindow->okButton, SIGNAL(clicked()), this, SLOT(clickedLoginOk()));
connect(loginWindow->cancelButton, SIGNAL(clicked()), this, SLOT(clickedLoginCancel()));
loginDialog->show();
}
void SmsGui::clickedLoginOk()
{
kill();
}
void SmsGui::kill()
{
loginDialog->close();
}
void SmsGui::clickedLoginCancel()
{
loginDialog->close();
}
void SmsGui::setMiastoPlusaParameters()
{
showLoginDialog();
}
To copy to clipboard, switch view to plain text mode
Bookmarks