View Full Version : QTimer and modeless dialog

2nd July 2012, 00:09

I was faced with a very strange behavior. Not critical as I can do it other way but I would like to know why it happens.
I have a qt designer class that I use as a modeless dialog In a mainwindow application.

If i defined in my mainwindow.h a pointer to a QTimer variable my application always gives segfault at the call of the modeless dialog show(), regardless i do not even use or mention my QTimer pointer anywhere else in the program. Any no modeless dialogs work just fine.
For my application to work I only need to comment the Qtimer *timer declaration in mainwindow.h

I have no problem if i just define the timer in any of the mainwindow functions in mainwindow.cpp, and that's why this problem is not critical, but for me, at least, is very strange. It took me a large amount of hours to discovered that the segfault was due to the fact that I had a QTimer pointer defined in mainwindow.h as I couldn't spot any relation with that declaration, not even used anywhere else, and the modeless dialog.

kind regards,

2nd July 2012, 08:27
I would suggest that this happens because of either
1) A corrupted build - try a clean and rebuild

2) A compiler or linker bug

3) Some other hidden bug is the real culprit; this piece of code just is a catalyst

...Of course, you should not have unused variables in your code... Perhaps the problem would go away if you used the variable.

2nd July 2012, 10:20
having an uninitialised pointer anywhere doesn't cause a seg fault inherently. I would suggest you still have a problem somewhere

2nd July 2012, 11:21
To say what the problem is, we should be able to see your code.

2nd July 2012, 15:46
To say what the problem is, we should be able to see your code.

I understand that. However it is a huge amount of code. I'm going to try to replicate the problem with a small ad-hoc application as I'm really worried about having an hidden bug completely unrelated. I'm assembling an application to be used by many co-workers in academic and scientific environment and it is really big.

If I'm able to replicate the problem with a smaller code sample I'll post it again.

Many thanks.

2nd July 2012, 16:17
Sometimes it is enough to run your program with a debugger and post the backtrace of the crash. Just be sure to build the application in debug mode.

2nd July 2012, 16:46
a full clean and a rebuild might also help.

2nd July 2012, 17:18
a full clean and a rebuild might also help.

I've tried that the first time I encountered the problem.
Meanwhile i was able to pinpoint the problem. This is the first time I use modeless dialogs and I was copying the qt documentation example

void EditorWindow::find()
if (!findDialog) {
findDialog = new FindDialog(this);
connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext()));

so I have a slot in my mainwindow

void MainWindow::select_singlevaluesfields()

channelldialog=new channelWindow(this);

QDateTime x,y;
x.setTimeSpec(Qt::UTC); y.setTimeSpec(Qt::UTC);


and in mainwindow .h I have defined

channelWindow *channelldialog;

It seems that sometimes the test !channelldialog is always false even in the first call (this occurs anytime I have also a QTimer pointer in the main window) so any uses of the channelldialog points to nowhere and the segfault. the problem goes away is i do not rely in the if(!channelldialog) condition test and add the channelldialog=new channelWindow(this); in the mainwindow constructor.

Added after 6 minutes:

oops, I've just noticed there is a huge difference between my code and the qt code example. My bad.

2nd July 2012, 17:23
It seems that sometimes the test !channelldialog is always false
Well, where did you initialize it?
If you have an uninitialized pointer, it indeed points to what ever garbage its address has when the variable is defined.
If you initialize it in your constructor, you wont have undefined behavior.

MyClass::MyClass() : MyBaseClass(),

will ensure your pointers is NULL at startup.

2nd July 2012, 17:35
The problem is now solved just added, as suggested high_flyer the initialized to NULL in mainwindow constructor the channelldialog pointer.

many thanks to him and to all the others that offered further insight into my problem.

Best wishes.