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(2)

Published on| May 14th, 2009 | No Comment.
Description:
About Qt app to debug, and easier, I wrote the article. (See related article above)
However, in the course of the article about memory leaks, some, or so I need to supplement (2) to write the article.
Qt, to verify the accuracy of memory leaks (at least Windows,) I may be the best choice to use the tools on the market. However, unable to get an expensive commercial tool (which is so too), how good is the feeling that something or not, I recently, about what to do, to write Let's think.

Articles: Qt apps with VC + + 2008 Express to debug

Reference article: http://www.codeproject.com/KB/debug/Memory_leak_finder.aspx
Reference article: http://www.codeguru.com/cpp/misc/misc/memory/article.php/c3745/

Sample source code you used here:


Let's output to a memory leak (2)
Previously, VC + + used to check the memory leaks that are available. However, this memory leak check, Qt and, basically, can not be correct for consistency, and can not be output in the last few have been described.

So, I think the other way.
When implemented in Windows, WinDbg to find the leak, as a means for free, I think it is well known.

However, VC + + as a person he is accustomed to, WinDbg commands to the script (which is like the same script commands) to do a little, and nothing seemed to set back the clock There are also TAKUNAI.

So, WinDbg something other than how good it is or not.
In fact, memory leak detection library of reference of the article linked, it is.

I like using the special power was not so bad, a little, I would like to present.
(Of course, there are bugs. )


The long preamble now, immediately, let me try.
First, the articles you download the source code for the WEB site demo.
The first reference of the article (codeproject) a, C + + is for. The latter (codeguru) is, C is for.
Each author is different, each a different way. However, none of the source code changes are not nearly. (To capture the environment is about the # include)
In addition, C + + for (codeproject) is, DLL Download the. (VC + +2008 + Qt environment, SUTATIKKURINKU, error-prone. )

Traceallocations.cpp download of 371 the following change to the line.
(I think it's probably a bug)

371
372
//    if (index <= MAXSTACK) {
    if (index < MAXSTACK) {

VC + +2008, compiled on a sample, please run.
If the following message output is OK.
(The file path to the source code will depend on their environment. )

Leak of 4 bytes detected (452 bytes with headers and no mans land)
     ...\memleakfinder4dll\tracealloc.cpp(8) : operator new
     ...\memleakfinder4dll\main.cpp(12) : generate_memoryleak
     ...\memleakfinder4dll\main.cpp(19) : main
     ...\crt_bld\self_x86\crt\src\crt0.c(266) : __tmainCRTStartup
     ...\crt_bld\self_x86\crt\src\crt0.c(182) : mainCRTStartup
     0x0012B7A4 : _BaseProcessStart@4

Let's check the source code that deals with the actual post.

VC + +, Qt and the application would change the project, Qt here because I've lost the integrity of the project, the following change to the source code to.
The application to Qt, tracealloc.cpp capture, MemLeakDll.lib a link to LIB.
In this case, where the DLL from the project, the following files in the Qt application under VC + + to copy the project directory.
  • tracealloc.cpp
  • MemLeakFindDll.h
  • MemLeakDll.lib
  • MemLeakDll.dll


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#if !defined(QT_NO_DEBUG) && defined(WIN32)
#define DETECT_LEAKS
#include "tracealloc.cpp"
#pragma comment(lib, "MemLeakDll.lib")
#endif
 
#include "QApplication"
#include "QPushButton"
 
int main(int argc, char *argv[])
{
 
 
 
    QApplication app(argc, argv);
    QPushButton hello("Hello world");
    hello.resize(100, 30);
 
	//	メモリリーク
	int *pint=new int[10];
 
    hello.show();
    return app.exec();
}

When you are ready, please try to compile and run.
As with the earlier demonstration, the output area "Leak of 40 bytes detected" I think the message was output.

Similarly, the following three articles where we will check samples from the same files.

  • 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.

The addition of hand, as well as samples of sample.cpp only.

[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
21
#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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#if !defined(QT_NO_DEBUG) && defined(WIN32)
#define DETECT_LEAKS
#include "tracealloc.cpp"
#pragma comment(lib, "MemLeakDll.lib")
#endif
 
#include "QApplication"
#include "QPushButton"
#include "MyWidget.h"
 
int main(int argc, char *argv[])
{
 
 
 
	QApplication app(argc, argv);
	MyWidget hello;
 
	hello.show();
	return app.exec();
}
Lines 17,19, only the screen.

When you are ready, please try to compile and run.
As with the earlier demonstration, the output area "Leak of 40 bytes detected" I think the message was output.

The
[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.

People can think of. As well as a display of menus and toolbar icons had a problem with the article. (Please see sample debugmemory3)
In this example, the methods of the previous article, I had a memory leak is detected, this method will not correct the error.

Even now, the library uses this memory leak detection in various forms, it seems to work correctly. However, this can be found in the library, new, delete can not be used to manage memory. In fact the C malloc, free to carry out detecting and using the reference article (codeguru) you can use. However, this library is the output file, the output of debugging information to the area, if you make changes to the original use of the library tried to, almost, MFC memory leak detection is possible as the I think so.
※ C-side is a little different way. malloc, free and in use, you must include a header to give.
※ CPP side, once you can capture, in the same project, will detect all the same.

I was like. Therefore, debugging, free environment, it was almost, Qt apps, you can create.

Comments

Leave a Reply







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