[SOLVED] SIGSEGV 11 - on close from the taskbar but not from the mainwindow (x) cloase button
-
I have a strange situation where I get a SIGSEGV with the int 11 on application close but only from the taskbar - Linux Kubuntu 14.04.
How can I manage this?- main.cpp
@#include <stdio.h>
#include <QDebug>
#if defined(Q_OS_LINUX)
#include <signal.h>
#endif#include "AtpSingleApplication.h"
#include "AtpSettings.h"
#include "AtpMainWindow.h"#if defined(Q_OS_LINUX)
void catchSigPipe(int s){
qDebug() << "SIGPIPE caught: " << s;
}
#endifvoid messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
QByteArray localMsg = message.toLocal8Bit();
// QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit();switch (type) {
case QtDebugMsg:
fprintf(stderr,/* "%s - / "[%s:%u, %s]\n Debug: %s \n", /currentTime.constData(),/ context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtWarningMsg:
fprintf(stderr, /"%s - / "[%s:%u, %s:]\n Warning: %s\n", /currentTime.constData(),/ context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtCriticalMsg:
fprintf(stderr, /"%s - / "[%s:%u, %s]\n Critical: %s\n", /currentTime.constData(),/ context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
break;
case QtFatalMsg:
fprintf(stderr, /"%s -*/ "[%s:%u, %s]\n Fatal: %s\n", /currentTime.constData(),/ context.file, context.line, context.function, localMsg.constData());
fflush(stderr);
abort();
}
}int main(int argc, char *argv[]){
//debug
qInstallMessageHandler(messageHandler);
//AppId
QString appId = "@atp@";
for (int i=0; i<argc; i++) {
QString a = argv[i];
if (a=="-app-id" && i+1<argc) {
appId = argv[i+1];
}
}
//start app
AtpSingleApplication *app = new AtpSingleApplication(appId, argc, argv);
app->setOrganizationName("atp");
app->setOrganizationDomain("atp-trading.com");
app->setApplicationName("@atp@");
app->setApplicationVersion("1");
app->setApplicationDisplayName("atp");
//sigPipe problems
#if defined(Q_OS_LINUX)
signal(SIGPIPE,catchSigPipe);
signal(SIGSEGV,catchSigPipe);
#endif//init settings
AtpSettings *mainSettings = new AtpSettings;//detect multiple instance
if (!mainSettings->getAppMultipleInstance()) {
// send arguments to running instance for further processing:
if (app->sendMessage(app->arguments().join("\n"), 30000)) {
qWarning("Application already running. Aborting...");
return 0;
}
}
//main window
AtpMainWindow win;
win.show();
//main run
return app->exec();
}
@- AtpMainWindow
@
AtpMainWindow::AtpMainWindow(QWidget *parent, bool hasMdiArea) : QMainWindow(parent){
qDebug()<<"Start AtpMainWindow" << hasMdiArea;
settingsMainWindow = new AtpSettings();
qDebug() << settingsMainWindow->fileName();
setWindowTitle("@atp@");
AtpSingleApplication singleApp = dynamic_cast<AtpSingleApplication> (qApp);
if (singleApp!=NULL) {
singleApp->setActivationWindow(this);
}
readSettings();
}AtpMainWindow::~AtpMainWindow(){
qDebug()<<"Exit AtpMainWindow";
}void AtpMainWindow::quit() {
emit closeRequested();
QMainWindow::close();
}//event on close mainWindow
void AtpMainWindow::closeEvent(QCloseEvent *e) {
qDebug()<<"Start closeEvent";
if (maybeSave()) {
writeSettings();
e->accept();
QApplication::quit();
} else {
e->ignore();
}
}//save any unsaved data
bool AtpMainWindow::maybeSave(){
return true;
}//Restores the application window settings (size, position, ...).
void AtpMainWindow::readSettings() {
qDebug()<<"readSettings";
qDebug() << settingsMainWindow->fileName();
QPoint windowPos = settingsMainWindow->getAppPosition();
QSize windowSize = settingsMainWindow->getAppSize();
resize(windowSize);
move(windowPos);
}// Stores the application window settings (size, position, ...).
void AtpMainWindow::writeSettings() {
qDebug()<<"writeSettings";
qDebug() << settingsMainWindow->fileName();
settingsMainWindow->setAppPosition(pos());
settingsMainWindow->setAppSize(size());
qDebug() << settingsMainWindow->fileName();
}
@I've done a lot of research.. nothing yet.. and what is curios is that is only from the taskbar.. how Qt manage thouse different?
- main.cpp
-
Hi,
From a quick look it seems fine, what does AtpSingleApplication look like ?
On a side note, you should use qobject_cast rather than dynamic_cast since you are casting from a QObject
-
- AtpSingleApplication
@AtpSingleApplication* AtpSingleApplication::instance = NULL;
void AtpSingleApplication::sysInit(const QString &appId) {
qDebug()<<"AtpSingleApplication sysInit";
mutexEventsLocker = NULL;
instance = this;
actWin = 0;
peer = new AtpLocalPeer(this, appId);
connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
}AtpSingleApplication::AtpSingleApplication(int &argc, char **argv) : QApplication(argc, argv) {
qDebug()<<"Start AtpSingleApplication";
sysInit("atp");
}AtpSingleApplication::~AtpSingleApplication(){
qDebug()<<"Exit AtpSingleApplication";
}AtpSingleApplication* AtpSingleApplication::getInstance() {
qDebug()<<"AtpSingleApplication getInstance";
return instance;
}bool AtpSingleApplication::isRunning() {
qDebug()<<"AtpSingleApplication isRunning";
return peer->isClient();
}bool AtpSingleApplication::sendMessage(const QString &message, int timeout) {
qDebug()<<"AtpSingleApplication sendMessage";
return peer->sendMessage(message, timeout);
}QString AtpSingleApplication::id() const {
qDebug()<<"AtpSingleApplication id";
return peer->applicationId();
}void AtpSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage) {
qDebug()<<"AtpSingleApplication setActiveWindow";
actWin = aw;
if (activateOnMessage)
connect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
else
disconnect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
}QWidget* AtpSingleApplication::activationWindow() const {
qDebug()<<"AtpSingleApplication activationWindow";
return actWin;
}void AtpSingleApplication::activateWindow() {
qDebug()<<"AtpSingleApplication activateWindow";
if (actWin) {
actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
actWin->raise();
actWin->activateWindow();
}
}bool AtpSingleApplication::event(QEvent* e) {
qDebug()<<"AtpSingleApplication event";
QFileOpenEvent* foe = dynamic_cast<QFileOpenEvent*>(e);
if (foe!=NULL) {
emit(fileOpenRequestReceived(foe->file()));
e->accept();
qDebug() << "AtpSingleApplication::event: type: " << e->type() << ": OK";
return true;
}bool ret = QApplication::event(e);
return ret;
}@ - AtpSingleApplication
-
output from close on taskbar
@[../../../electricity/src/main/main.cpp:15, void catchSigPipe(int)]
Debug: SIGPIPE caught: 11
[../../../electricity/src/main/main.cpp:15, void catchSigPipe(int)]
Debug: SIGPIPE caught: 11
[../../../electricity/src/main/main.cpp:15, void catchSigPipe(int)]
Debug: SIGPIPE caught: 11
[../../../electricity/src/main/main.cpp:15, void catchSigPipe(int)]
Debug: SIGPIPE caught: 11
[../../../electricity/src/main/main.cpp:15, void catchSigPipe(int)]
Debug: SIGPIPE caught: 11
/home/andrei/Qt-projects/electricity/debug/electricity-bin exited with code 0@output from close (x) button
@ Debug: "/home/andrei/Qt-projects/electricity/debug/atp.ini"
[../../../electricity/src/AtpGui/AtpMainWindow.cpp:66, void AtpMainWindow::writeSettings()]
Debug: "/home/andrei/Qt-projects/electricity/debug/atp/atpElectricity.ini"
[../../../electricity/src/AtpGui/AtpMainWindow.cpp:25, virtual AtpMainWindow::~AtpMainWindow()]
Debug: Exit AtpMainWindow
/home/andrei/Qt-projects/electricity/debug/electricity-bin exited with code 0@why this difference?
-
Did you try to run it in a debugger ?
You might be using something that has been already destroyed.
Also you have a memory leak in your main. you never delete your app.
On a side note Qt already proposes QtSingleApplication in its "Solution" catalog.
-
bq. On a side note Qt already proposes QtSingleApplication in its “Solution” catalog.
yes is true I've found it "here":http://doc.qt.digia.com/solutions/4/qtsingleapplication/qtsingleapplication.html
but not in Qt 5.3 so I can not do like #include <QSingleApplication> and because of that I will use mine one.. witch is not perfect but tried to copy some info from there... -
The solutions are not part of either Qt 4 or 5 you have to download them and integrate them in your project
-
ahammm.. thanks.. I'll try this... never knew about this any way about memory leak as far as i remember I've read somewere that Qt will manage this and close everithing for me... can you bring a good example of how I should close it not to have that memory leak? it is possible to receive because of that SIGSEGV?
-
Qt does indeed help with memory management through e.g QObject parent/child handling but it doesn't replace everything.
@
int main(int argc, char *argv[]){
//snip
AtpSingleApplication *app = new AtpSingleApplication(appId, argc, argv); << allocating the application on the heap
app->setOrganizationName("atp");
app->setOrganizationDomain("atp-trading.com");
app->setApplicationName("@atp@");
app->setApplicationVersion("1");
app->setApplicationDisplayName("atp");
//snip again
AtpMainWindow win;
win.show();
return app->exec(); << you don't delete the application, Qt can't do it for you.
}
@ -
by the way I've run the program in windows and bealiveme no SIGSEGV. usualy in windows there is a message box but everithing was fine.. wat is mre anoing is why is troing out SIGSEGV if the return is 0 - app finish corectly hm... have to do more reasearch - only on linux enviroment
-
Memory management is not the same in both OS. Did you run it using a debugger ? What happens if you delete the app object by hand ?
-
- main.cpp
@//main run
int ret = app->exec();
qDebug()<< "Del app"; //just to monitor the behaviour
delete app;
qDebug()<< "Del app after"; //just to monitor the behaviour
return ret;
@
this is perfect under linux exit with 0 end no signal caught;
- main.cpp
-
Are you can just create app on the stack
-
sorry but i do not understand what do you want to say.
bq. Are you can just create app on the stack
what that means?? Sorry but englisn is not my native language -
Search for "stack vs heap"