PDA

View Full Version : using assert, assertion failed, QTimerEvent keeps getting called! (qt3.3.8, vs2005)



roybj
15th February 2011, 23:12
using qt3.3.8, vs2005 sp1

hi, i have narrowed my problem to the following:
created the simplest qt program, with QApplication and a simple qobject with QTimerEvent and in the timer event i called assert(0) to raise an assertion failure.
started the timerEvent and found that the timerevent keeps getting called (and i get an assertion failure again and again)
of course this is not desirable, i want the program to stop.
all the code up to the assertion (and non after) will be called again and again.
any ideas?

thanks for you help,

Roy.

jonspieg
17th February 2011, 12:38
The same thing happened to me too!
I tried using Q_ASSERT macro, but with no success. when I called abort() directly to terminate the proccess, it rendered the same results.
When I tried this with QT 4.5 it worked as desired, however I'm obliged to use QT 3.3.8 in my workplace.

could anyone reproduce this behaviour?
is there a solution to this problem?

thanks,
Jonathan

norobro
17th February 2011, 17:48
From qglobal.h in the Qt 3.3.8 source code:
#if !defined(Q_ASSERT)
# if defined(QT_CHECK_STATE)
# if defined(QT_FATAL_ASSERT)
# define Q_ASSERT(x) ((x) ? (void)0 : qFatal("ASSERT: \"%s\" in %s (%d)",#x,__FILE__,__LINE__))
# else
# define Q_ASSERT(x) ((x) ? (void)0 : qWarning("ASSERT: \"%s\" in %s (%d)",#x,__FILE__,__LINE__))
# endif
# else
# define Q_ASSERT(x)
# endif
#endif

jonspieg
22nd February 2011, 09:44
Thanks for your reply.

I'm aware of the definition of Q_ASSERT but I don't see how this can help me.
I also tried calling qFatal(), but still the results are the same.

Do you have any idea how to solve it, and change the behavior of assertion in qt 3.3.8 loop?

Thanks.

norobro
22nd February 2011, 15:12
As you can see from the source code if QT_FATAL_ASSERT isn't defined your app will call qWarning. To make your program exit (i.e. call qFatal) upon an assert failure you need to define the flag.

You can put the following statement as the first line of your .cpp file:
#define QT_FATAL_ASSERT
Or put the following in your .pro file:
DEFINES += QT_FATAL_ASSERT

jonspieg
22nd February 2011, 16:08
I tried defining QT_FATAL_ASSERT and I got an assertion failed window, but the program is still running and the timer is still getting called.

Here is a simple example of what I'm trying to do:


#include <cassert>
class MyObject : public QObject
{
public:
MyObject() { startTimer(100); }

virtual void timerEvent(QTimerEvent* e)
{
cout<<"beforeAssert";
assert(0);
cout<<"afterAssert";
}
};

...

int main(int argc, char** argv)
{
QApplication qApp(argc, argv);

MyObject object;

return (qApp.exec());
}


this code prints "beforeAssert" then pops an assertion failed window, but instead of stopping there it keeps on printing ("beforeAssert") and popping messages, until I manually kill the process.

note: anything after the assert will not be called (i.e "afterAssert will not be printed")

norobro
22nd February 2011, 16:55
QT_FATAL_ASSERT works with Q_ASSERT.

The following works on my box:

#define QT_FATAL_ASSERT
#include <qapplication.h>
#include <qobject.h>

class Object : public QObject
{
Q_OBJECT
public:
Object(){
startTimer(1000);
}

void timerEvent(QTimerEvent *event){
Q_ASSERT(0);
}
};

int main(int argc, char *argv[]){

QApplication app(argc, argv);
Object obj;
app.exec();
}

#include "main.moc"


And so does your app with modifications:
#include <cassert>
#include <qobject.h>
#include <qapplication.h>
#include <iostream>

class MyObject : public QObject
{
public:
MyObject() { startTimer(100); }

virtual void timerEvent(QTimerEvent* e)
{
std::cout<<"beforeAssert" << std::endl;
assert(0);
std::cout<<"afterAssert";
}
};


int main(int argc, char** argv)
{
QApplication app(argc, argv);

MyObject object;

return (app.exec());
}

jonspieg
22nd February 2011, 19:23
are you using QT 3.3.8?

norobro
22nd February 2011, 20:08
are you using QT 3.3.8?

Yep. Qt 3.3.8 on a Debian Sid machine. Does the first program in post #7, with Q_ASSERT, not halt?

Maybe it's a Windows thang ;)

jonspieg
22nd February 2011, 21:19
yep. just won't halt...
thanks for the help though :)

maybe someone else can reproduce this in a windows environment?