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.
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でシェアする
ページトップへ