Gtest color information output + boost. Test Memory leakage detection and locating
/* Author : Andrew. Wu[Created on: 2009/04/29-Last modified on: 2009/05/02]
*/
Gtest's color information display looks eye-catching, boost. The test memory leakage check is very useful. I really hope that the two functions can be saved in one library, saving a lot of trouble. However, if you want to use these two functions before the authors do not, try the following methods.
1: # Include < Iostream>
2:
3: # Include < Gtest / Gtest . H >
4: # Pragma comment ( Lib, "Gtestd_d.lib" )
5:
6: # Include < Boost / Test / Unit_test . HPP >
7:
8: Test ( Footest , Test )
9: {
10: Char * T = New Char [ 2 ] ;
11: Expect_eq ( 3 , 3 ) ;
12: Expect_eq ( 2 , 3 ) ;
13: }
14:
15: Using boost : : Unit_test : : Test_case ;
16: Void Testf( )
17: {
18: Char * T = New Char [ 2 ] ;
19: Boost_check ( 3 = = 3 ) ;
20: Boost_check ( 2 = = 3 ) ;
21: }
22:
23: Using boost : : Unit_test : : Test_suite ;
24: Test_suite * Init_unit_test_suite ( Int Argc, Char * Argv [ ] )
25: {
26: STD : : Cout < < "Gtest... \ n" ;
27: : : Testing : : Initgoogletest ( & Argc , Argv ) ;
28: : : Testing : : Unittest : : Getinstance ( ) - > Run ( ) ;
29:
30: STD : : Cout < < "\ N boost. Test... \ n" ;
31: Test_suite * TS = Boost_test_suite ( "" ) ;
32: TS - > Add ( Boost_test_case ( & Testf ) ) ;
33: Return TS ;
34: }
(Only use gtest when using test case. Both of them are written for comparison only)
Gtest 1.3.0
Http://code.google.com/p/googletest/
Booost 1.8.0
Http://www.boost.org/
Output:
Windows MFCProgramLocate an instance:
Scenario 1:
Detected memory leaks!
Dumping objects->
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ strcore. cpp (141): {1081} normal block
At 0x0038f448, 30 bytes long.
Data: <x> 0C 00 E5 78 06 00 00 00 06 00 00 00 01 00 00 00
F: \ dd \ vctools \ vc7libs \ ship \Atlmfc \ SRC \ MFC \ inet. cpp(509):{1080} Client block
0x01268148, subtype C0, 28 bytes long.
AChttpconnection objectAt $01268148, 28 bytes long
Object dump complete.
--
Inet. cpp(509):{1080}
Memory allocated by 509th rows of inet. cpp
Specified object allocation order number 1080
--
Find the local file
C: \ Program Files \ Microsoft Visual Studio 9.0 \ Vc \ atlmfc \ SRC \ MFC \ inet. cpp
Find the corresponding row
Chttpconnection * cinternetsession: gethttpconnection (lpctstr pstrserver,
Internet_port nport/* = Internet_invalid_port_number */ ,
Lpctstr pstrusername /* = NULL */ , Lpctstr pstrpassword /* = NULL */)
{
Assert (Afxisvalidstring (pstrserver ));
// Line 509
Chttpconnection * presult = New Chttpconnection (this,
Pstrserver, nport, pstrusername, pstrpassword, m_dwcontext );
Return Presult;
}
In this case, the caller with this function did not clear the returned chttpconnection *, and the search call code found that this was true.
Scenario 2:
There is no file name or row number in the memory leak detection information.
Detected memory leaks!
Dumping objects->
{1080} Normal block at 0x00c28148, 1 bytes long.
Data: <> Cd
Object dump complete.
Add _ crtsetbreakalloc (1080);
--
start debugging
the memory allocation location (but not accurate) will stop and prompt
xxx.exe has triggered a breakpoint
Of course, there are still file names and row numbers to save trouble. Try adding them to the appropriate position of the file.
# Ifdef_ Debug
# Define newDebug_new
# Endif
// # Define debug_new (this_file, _ line __)
So we can see the file name and row number as above
Detected memory leaks!
Dumping objects->
D: \ work \ XXX. cpp (167)
:{ 1080} normal block at 0x00c28148, 1 bytes long.
Data: <> Cd
Object dump complete.
Sets a breakpoint on a specified object allocation order number (debug version only ).
--