PDA

View Full Version : Using QT inside boost unit test - how to call QCoreApplication?



cdguenther
16th March 2010, 19:08
Hi,
I am developing QT based non-GUI code (using QT events, shared memory, logging, etc). I want to unit test this code using boost unit test framework. The tests all pass ok, but when the test suite ends there are memory leaks. I think this is caused by the QT message loop not being run so QObject (QFile, QDir, etc) instances are not being freed. What I can't figure out is how to run QCoreApplication.

I do not have access to the program main function - it is hidden away inside boost somewhere. How do I call QCoreApplication so that QObjects are properly destroyed and the boost test framework properly runs?

I am using:
QT: 4.6.2
Boost 1.42
Windows Visual Studio 2008 SP1 Version 9.0.30729.1
Microsoft .NET 3.5 SP1
Windows XP SP3

I'm calling this from the beginning of test case 1 - it never returns.



int initQT()
{
BOOST_TEST_MESSAGE("Initializing QT...");

// fake some data for QCodeApplication

int argc = 1;
char argv1[] = "IRL_QT.exe";
char* argv[] = {argv1, NULL};
QCoreApplication qca(argc, argv);
return qca.exec();
}


With memory leaks errors:

1>Detected memory leaks!
1>Dumping objects ->
1>{253997} normal block at 0x0091D968, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{253996} normal block at 0x0091C0A8, 4 bytes long.
1> Data: <h > 68 D9 91 00
1>{252618} normal block at 0x00910928, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{252617} normal block at 0x003EFB60, 4 bytes long.
1> Data: <( > 28 09 91 00
1>{251898} normal block at 0x00912FC8, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{251897} normal block at 0x00912F98, 4 bytes long.
1> Data: < / > C8 2F 91 00
1>{251677} normal block at 0x00913030, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{251676} normal block at 0x009112F8, 4 bytes long.
1> Data: <00 > 30 30 91 00
1>{221032} normal block at 0x0092B108, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{221011} normal block at 0x0092A0D8, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{220990} normal block at 0x009290A8, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{220969} normal block at 0x00928078, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{220948} normal block at 0x00927048, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{220927} normal block at 0x00926018, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{198116} normal block at 0x009176A8, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{198095} normal block at 0x00916678, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{198074} normal block at 0x00913160, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{129667} normal block at 0x00916630, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{129666} normal block at 0x00915358, 4 bytes long.
1> Data: <0f > 30 66 91 00
1>{128143} normal block at 0x00910528, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{128142} normal block at 0x003E79F0, 4 bytes long.
1> Data: <( > 28 05 91 00
1>{127285} normal block at 0x00924FE8, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{127264} normal block at 0x009141A0, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{127243} normal block at 0x009113B8, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{119426} normal block at 0x00912C88, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{119425} normal block at 0x003E7570, 4 bytes long.
1> Data: < , > 88 2C 91 00
1>{117904} normal block at 0x0091C6B0, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{117903} normal block at 0x0091AFF8, 4 bytes long.
1> Data: < > B0 C6 91 00
1>{94218} normal block at 0x0091B028, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{94197} normal block at 0x00919F48, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{94176} normal block at 0x009105D0, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{94175} normal block at 0x003E81A0, 4 bytes long.
1> Data: < > D0 05 91 00
1>{94174} normal block at 0x00918F18, 4097 bytes long.
1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
1>{47534} normal block at 0x00910980, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{47533} normal block at 0x009101B8, 4 bytes long.
1> Data: < > 80 09 91 00
1>{1011} normal block at 0x00910EA0, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{1010} normal block at 0x003E73C8, 4 bytes long.
1> Data: < > A0 0E 91 00
1>{766} normal block at 0x00910890, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{765} normal block at 0x003E83A8, 4 bytes long.
1> Data: < > 90 08 91 00
1>{764} normal block at 0x00910638, 552 bytes long.
1> Data: < H > 83 00 00 00 83 00 00 00 48 06 91 00 CD CD CD CD
1>{753} normal block at 0x00910400, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{752} normal block at 0x00910360, 116 bytes long.
1> Data: <xT)g( 9g> 78 54 29 67 28 03 91 00 00 00 00 00 C0 E8 39 67
1>{751} normal block at 0x00910328, 8 bytes long.
1> Data: <8T)g` > 38 54 29 67 60 03 91 00
1>{750} normal block at 0x009102E0, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{749} normal block at 0x00910298, 24 bytes long.
1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
1>{748} normal block at 0x00910230, 56 bytes long.
1> Data: < ( > 01 00 00 00 28 03 91 00 00 CD CD CD 00 00 00 00

Thanks,
Clarence

learningqt
7th May 2012, 13:39
did you get a solution???

amleto
7th May 2012, 14:45
int initQT()
{
BOOST_TEST_MESSAGE("Initializing QT...");

// fake some data for QCodeApplication

int argc = 1;
char argv1[] = "IRL_QT.exe";
char* argv[] = {argv1, NULL};
QCoreApplication qca(argc, argv);
return qca.exec();
}

learning, the op should have added something to the test that will call quit/exit on the application to make it end.

learningqt
7th May 2012, 15:12
MY code is this.


#include <QApplication>
#include <QPalette>
#include <QPixmap>
#include <QSplashScreen>
#include <qthread.h>

#include "boost/scoped_ptr.hpp"
#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_ALTERNATIVE_INIT_API
#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;

#include "MainWindow.h"

class I : public QThread
{
public:
static void sleep(unsigned long secs) {
QThread::sleep(secs);
}
};


BOOST_AUTO_TEST_SUITE( test_suite_1 )

BOOST_AUTO_TEST_CASE( test_case_1 )
{
BOOST_MESSAGE( "Testing is in progress" );

BOOST_CHECK( false );
}

BOOST_AUTO_TEST_SUITE_END()

bool init_function()
{
return true;
}

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

QApplication app(argc, argv);


QPixmap pixmap(":/images/betaLogo.png");
QSplashScreen splash(pixmap);
splash.show();
splash.showMessage("Loading...");
qApp->processEvents();
I::sleep(2);//delay 2 sec
///Palette color setting for the MainWindow
QPalette pal = app.palette();
pal.setColor(QPalette::Window, Qt::darkGray);
app.setPalette(pal);
///Calling Main Window
MainWindow window("artists");
window.show();
//Makes the splash screen wait until the widget mainWin is displayed
splash.finish(&window);
//app.exec();
return ::boost::unit_test::unit_test_main( &init_function, argc, argv );

}

I am now getting a linker error


main.cpp.4.o : error LNK2019: unresolved external symbol "int __cdecl boost::unit_test::unit_test_main(bool (__cdecl*)(void),int,char * * const)" (?unit_test_main@unit_test@boost@@YAHP6A_NXZHQEAPE AD@Z) referenced in function main
C:\cygwin\home\sobingt\tunebasket\p2p\build\test\U I\app.exe : fatal error LNK1120: 1 unresolved externals

can u help me ??

amleto
7th May 2012, 16:39
yes, read boost docs on test suite. boost test is nothing to do with Qt.