forked from SpatioTemporal/STARE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memleak.c
193 lines (149 loc) · 5.68 KB
/
memleak.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*****************************************************************
* EXAMPLE 1 *
* This simple program illustrates the basic debugging features *
* of the C runtime libraries, and the kind of debug output *
* that these features generate. *
*****************************************************************/
#include <stdio.h>
#include <string.h>
// #include <malloc.h>
#include <stdlib.h>
// Microsoft memory leak checking dependency.
#include <crtdbg.h>
// This routine place comments at the head of a section of debug output
void OutputHeading( const char * explanation )
{
_RPT1( _CRT_WARN, "\n\n%s:\n**************************************\
************************************\n", explanation );
}
// The following macros set and clear, respectively, given bits
// of the C runtime library debug flag, as specified by a bitmask.
#ifdef _DEBUG
#define SET_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define CLEAR_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define SET_CRT_DEBUG_FIELD(a) ((void) 0)
#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif
static _CrtMemState MS1, MS2, MS3;
void ckp1(){
// Send all reports to STDOUT
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
_CrtMemCheckpoint( &MS1 );
}
void ckp2(){
_CrtMemCheckpoint( &MS2 );
}
void s2s1(){
OutputHeading(" compare s2 s1");
if ( _CrtMemDifference( &MS3, &MS1, &MS2 ) )
_CrtMemDumpStatistics( &MS3 );
// _CrtDumpMemoryLeaks( );
OutputHeading(" compare s2 s1 END");
}
void memleak()
{
char *p1;
// *p2;
// p1 = malloc (1000);
// p2 = malloc( 38 );
// strcpy( p2, "This new p2 string occupies 38 bytes.");
// free(p2);
// be free(p1);
p1 = malloc(1024);
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
OutputHeading( "ck1 and ck2 changes" );
free(p1);
ckp1();
// p1 = malloc(64);
ckp2();
s2s1();
OutputHeading("again");
ckp2();
s2s1();
}
void memleak_bakXXX( )
{
char *p1, *p2;
_CrtMemState s1, s2, s3;
// Send all reports to STDOUT
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
// Allocate 2 memory blocks and store a string in each
p1 = malloc( 34 );
strcpy( p1, "This is the p1 string (34 bytes)." );
p2 = malloc( 34 );
strcpy( p2, "This is the p2 string (34 bytes)." );
OutputHeading(
"Use _ASSERTE to check that the two strings are identical" );
_ASSERTE( strcmp( p1, p2 ) == 0 );
OutputHeading(
"Use a _RPT macro to report the string contents as a warning" );
_RPT2( _CRT_WARN, "p1 points to '%s' and \np2 points to '%s'\n", p1, p2 );
OutputHeading(
"Use _CRTMemDumpAllObjectsSince to check the p1 and p2 allocations" );
_CrtMemDumpAllObjectsSince( NULL );
free( p2 );
OutputHeading(
"Having freed p2, dump allocation information about p1 only" );
_CrtMemDumpAllObjectsSince( NULL );
// Store a memory checkpoint in the s1 memory-state structure
_CrtMemCheckpoint( &s1 );
// Allocate another block, pointed to by p2
p2 = malloc( 38 );
strcpy( p2, "This new p2 string occupies 38 bytes.");
// Store a 2nd memory checkpoint in s2
_CrtMemCheckpoint( &s2 );
OutputHeading(
"Dump the changes that occurred between two memory checkpoints" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );
// Free p2 again and store a new memory checkpoint in s2
free( p2 );
_CrtMemCheckpoint( &s2 );
OutputHeading(
"Now the memory state at the two checkpoints is the same" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );
strcpy( p1, "This new p1 string is over 34 bytes" );
OutputHeading( "Free p1 after overwriting the end of the allocation" );
free( p1 );
// Set the debug-heap flag so that freed blocks are kept on the
// linked list, to catch any inadvertent use of freed memory
SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF );
p1 = malloc( 10 );
free( p1 );
strcpy( p1, "Oops" );
OutputHeading( "Perform a memory check after corrupting freed memory" );
_CrtCheckMemory( );
// Use explicit calls to _malloc_dbg to save file name and line number
// information, and also to allocate Client type blocks for tracking
p1 = _malloc_dbg( 40, _NORMAL_BLOCK, __FILE__, __LINE__ );
p2 = _malloc_dbg( 40, _CLIENT_BLOCK, __FILE__, __LINE__ );
strcpy( p1, "p1 points to a Normal allocation block" );
strcpy( p2, "p2 points to a Client allocation block" );
// You must use _free_dbg to free a Client block
OutputHeading(
"Using free( ) to free a Client block causes an assertion failure" );
free( p1 );
free( p2 );
p1 = malloc( 10 );
OutputHeading( "Examine outstanding allocations (dump memory leaks)" );
_CrtDumpMemoryLeaks( );
// Set the debug-heap flag so that memory leaks are reported when
// the process terminates. Then, exit.
OutputHeading( "Program exits without freeing a memory block" );
SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
}