Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt apps with VC + + 2008 Express to debug

Published on| May 11th, 2009 | No Comment.
Description:
About Qt preferences, recently wrote the article. (See related article above)
So, to some extent, VC + + with Qt SOROIMASHITA development to the environment. So, will actually create a Qt application, the first question is, do you want to debug? I would think. In fact, until now for programmers who have created a MFC application and the level of environmental否MEMASEN down. And in creating applications using MFC, the APURIKESHONUIZADO It has, accordingly, SURURE create a template, you need to debug macros in the template that is to some extent, they are embedded. (Unfortunately Qt, you must fill in KOMANAKERE itself. )
And also say, MFC you have experience of, well if you add a macro name that is familiar to quite a memory leak, MFC and Win32 normal memory leak tests are used in applications unusable.

Around the area, which is what I try and write.

Related Articles: VC + + 2008 Express and using the sample compiled Qt Preferences
Related Articles: qmake in VC + + 2008 Express to create a project file

Reference article: Debugging Techniques

Sample source code you used here:


Qt's debug macros and output message
Qt macro for debugging output and the message may include the following.
Macro name Features
Q_ASSERT (cond): (* 1) cond is a boolean value (and judging). cnnd FALSE if the output of the following warning:
"ASSERT: 'cnnd' in file xyz.cpp (234) "
Q_ASSERT_X (cond, where, what): (* 1) cond is the same as above. where the output "and where" information. what is the output to "what" information. Output the following warning:
"ASSERT failure in where: 'what', file xyz.cpp, line 234 "
Q_CHECK_PTR (ptr): (* 1) ptr is a pointer. If ptr is null, then output the following warning:
"In file xyz.cpp, line 234: Out of memory "
qDebug (): (* 2) Displays the output for debugging and testing.
qWarning (): (* 3) Outputs a warning when the error occurred in the program.
qCritical () Critical error (system error) occurs when the error message output.
qFatal (): Quit fatal error message output.

The above macro MFC Qt equivalent to write the following macro name.
Qt macro name MFC macro name
Q_ASSERT (cond): (* 1) ASSERT (cond)
Q_ASSERT_X (cond, where, what): (* 1) ASSERT (cond) + TRACE ()
Q_CHECK_PTR (ptr): (* 1) ASSERT_VALID (pOb) or ASSERT_POINTER (p, type)
qDebug (): (* 2) TRACE () or AfxDump ()
qWarning (): (* 3) TRACE () or AfxDump () or fprinf (error)
qCritical () TRACE () or AfxDump () or FormatMessage? or fprinf (error)
qFatal (): TRACE () + FatalExit / FatalAppExit

* 1: QT_NO_DEBUG if defined (Release Version, etc.), do nothing.
* 2: QT_NO_DEBUG_OUTPUT if defined (Release Version, etc.), do nothing.
* 3: QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT if defined (Release Version, etc.), do nothing.

The output for each message, # include < QDebug> Can be described as follows to include the.
※ normal message output, printf is the same argument.

1
	qDebug() << "Debug << " << &hello << "Position:" << hello.pos();

Destination of the message, VC + +, running in debug, debug console (debugging output on the screen) to be output, MFC is the same.

Previous article "VC + + 2008 Express using the Qt compilation and sample preferences" sample.cpp used in the output and try to actually use the debugging information.

Let's use the debug macro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "QApplication"
#include "QPushButton"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton hello("Hello world");
    hello.resize(100, 30);
 
	int ntest=10;
	int *pttest=NULL;
 
	Q_ASSERT(ntest==10);
	Q_ASSERT(ntest!=10);
	Q_ASSERT_X(ntest!=10,"this is ntest!=10."," ? I do't know.");
	Q_CHECK_PTR(pttest);
 
	hello.show();
    return app.exec();
}

Been compiled debug version of the source code of the above, please try to run debug.
In lines 14-15, and the following screen appears.
Here, we confirm that the debugging information, "neglect" please click.


VC + + This is a string of the output of the following information to the debug output.
ASSERT: "ntest!=10" in file .\sample.cpp, line 14
ASSERT failure in this is ntest!=10.: " ? I do't know.", file .\sample.cpp, line 15
In file .\sample.cpp, line 16: Out of memory

Line 13 is correct, and through, 14,15,16 and the errors in the discharge line.

Also been compiled release version, please try to run debug.
Are not output anything, without having to break the normal "Hello world" and a button that appears.

Like the MFC, in the release version, please note that you will be unchecked.

Let's use the output message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifdef QT_NO_DEBUG
#define QT_NO_DEBUG_OUTPUT
//#define QT_NO_WARNING_OUTPUT
#endif
//	qDebug() << "Debug" のような記述を行う場合のみincludeする
#include <QDebug>
 
#include "QApplication"
#include "QPushButton"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton hello("Hello world");
    hello.resize(100, 30);
 
	//	デバッグメッセージを出力する
	qDebug("test-Debug");
	qWarning("test-Warning");
	qCritical("test-Critical");
	qFatal("test-Fatal");
 
//	必ず、QT_NO_DEBUG_OUTPUTをチェックする(コンパイルエラーになる)
#ifndef QT_NO_DEBUG_OUTPUT
	qDebug() << "Debug << " << &hello << "Position:" << hello.pos();
#endif
 
	hello.show();
    return app.exec();
}

1 - 7, please note that it is added to the line.
1 - 4 line, the release version, debug / whether to output a warning message.

2 In line with the release version, and have vowed not to output debugging messages.
The third line (I have commented out) in the release version, and have vowed not to output a warning message.


Been compiled debug version of the source code of the above, please try to run debug.
21 in line, I get the following screen.
Here, we confirm that the debugging information, "neglect" please click.


VC + + This is a string of the output of the following information to the debug output.
test-Debug
test-Warning
test-Critical
test-Fatal
Debug <<  QPushButton(0x12feb4) Position: QPoint(0,0) 

Also been compiled release version, please try to run debug.
Without a break, and I close the application immediately.

In the release version, qFatal in and stop the application immediately.
In fact, the debugging information, I think that the following message is printed.

test-Warning
test-Critical
test-Fatal

Thus the MFC debug information can be output to the same.
So, C / C + +, for a memory leak problem that most often? Qt, you should do?

Let's output to a memory leak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#if !defined(QT_NO_DEBUG) && defined(WIN32)
#include <crtdbg.h>
#endif
 
#include "QApplication"
#include "QPushButton"
 
int main(int argc, char *argv[])
{
#if !defined(QT_NO_DEBUG) && defined(WIN32)
    _CrtSetDbgFlag( _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF );
#endif
    QApplication app(argc, argv);
    QPushButton hello("Hello world");
    hello.resize(100, 30);
 
	//	メモリリーク
	int *pint=new int[10];
 
    hello.show();
    return app.exec();
}

WIN32 I generally put the process in the same way as when you check the memory leak.
As a result, output was as follows.

Detected memory leaks!
Dumping objects -->
{1691} normal block at 0x00AD0320, 40 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.

On our results.
If you know the Qt, Qt3, I was reminded that the output of the mass was a memory leak in the source code.
(VC + +2008 and Qt4.5.1 in combination, and the above results. )
Well, Qt provides a mechanism to remove the object that is managed by itself automatically.

For example,
If you have a QWidget QPushButton Show,
QPushButton is, new is created, the object placed QWidget.
Later, QWidget when it is destroyed itself, where QPushButton also be discarded, the user, delete is not as good.

Let's use the sample source code represents this example.

  • MyWidget.h
    - The main screen (the header), has only one button to exit.
  • MyWidget.cpp
    - The main screen (source), has only one button to exit.
  • sample.cpp
    - To display the main screen, simply quit.


[MyWidget.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef _MYWIDGET_H_
#define _MYWIDGET_H_
 
#include "QApplication"
#include "QPushButton"
#include "QVBoxLayout"
 
////////////////////////////////////////
//	メモリーリーク検証のためのデータクラス
class LocalData
{
public:
	LocalData();
	int m_ndata;
};
 
////////////////////////////////////////
//	メモリーリーク検証のための画面クラス
class MyWidget : public QWidget
{
public:
	MyWidget( QWidget *parent=0 ) ;
 
 
	QPushButton *m_pquit;	//	ボタンをnewする
	QVBoxLayout *m_playout;	//	レイアウトをnewする
	QPushButton *m_pquit2;	//	ボタンをnewする
 
	LocalData *m_plocaldata;	//	ローカルデータをnewする
};
#endif
Lines 10-15 are for testing memory leaks Qt defines a class-independent.

[MyWidget.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "MyWidget.h"
 
////////////////////////////////////////
//	メモリーリーク検証のためのデータクラス
LocalData::LocalData ()
{
	m_ndata=99;
}
 
 
////////////////////////////////////////
//	メモリーリーク検証のための画面クラス
MyWidget::MyWidget( QWidget *parent) : QWidget( parent, 0 )
{
	setMinimumSize( 200, 100 );
	setMaximumSize( 400, 400 );
 
	//	ボタンをnewする
	m_pquit   = new QPushButton( "Quit", this );
	//	レイアウトをnewする
	m_playout = new QVBoxLayout;
	m_playout->addWidget(m_pquit);
	setLayout(m_playout);
 
	//	関連のないボタンをnewする
	//	*****このオブジェクトは、メモリーリークと判断される
	m_pquit2   = new QPushButton( "Quit2");
 
	//	ローカルデータをnewする
	//	*****このオブジェクトは、メモリーリークと判断される
	m_plocaldata = new LocalData;
	Q_ASSERT(m_plocaldata->m_ndata==99);
 
	connect( m_pquit, SIGNAL(clicked()), qApp, SLOT(quit()) );
	setWindowTitle(QString ("test dialog"));
}
19 line, then paste the screen actually "Quit" button creates a (new) and.
Line 28, do not paste the screen "Quit2" button creates a (new) and.
32 line, Qt is not dependent on the generation of individual class members in this screen as a (new) and.


[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#if !defined(QT_NO_DEBUG) && defined(WIN32)
#include <crtdbg.h>
#endif
 
#include "QApplication"
#include "QPushButton"
#include "MyWidget.h"
 
 
int main(int argc, char *argv[])
{
#if !defined(QT_NO_DEBUG) && defined(WIN32)
	_CrtSetDbgFlag( _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF );
#endif
	QApplication app(argc, argv);
	MyWidget hello;
 
	hello.show();
	return app.exec();
}
Lines 16,18, only the screen.


So if you compile a debug version of this result and try to run debug, the output will leak memory.

[MyWidget.cpp] 26-33 and comment out the line up, result and try to run debug as well, this output is no longer any memory leaks.

Qt, clean? And let someone have a link to, correctly, that can output to a memory leak.
Qt is available in a few samples of the pieces, I tried, at first glance, the output was not a memory leak.
A little bit, I think I was not able to use this memory leak check function.

About Qt memory leaks, while I say a problem in the past, how to check, seems to have a discussion. The Linux system is free MEMORY CHECK TOOL "Valgrind", it seems to have been used.
Windows, MEMORY CHECK TOOL free. (If you know you IRASSHARE, please drop me a line. )
Expensive fee? And "Insure + +" was required.
Sure, you may effectively use the package for a fee, compared to the previous lot, Qt also had the impression that Windows Does the environment have been well-blended. (In the above method, where the many questions I can use. )

In this article, email them to me some comments would be appreciated.

Additional Notes: The sample used in Toobar icons and menus, you can not work. Yet, so there may be some other pattern that does not work, if you have this MEMORY LEAK CHECK, 10 minutes, please.
In Qt, MFC and Mixed when I went to, and errors.

※ There was a bug in the program since the last sample, I have to correct. If you download, I'm sorry.

Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ