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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
include(definitions.m4)dnl
__HTMLHEADER
__PAGEHEADER
__PAGESTART
<H2>Tutorial 7: Advanced examples</H2>
<P>In this tutorial you will learn how to make memory allocations
«invisible» so that they will not show up in the
Allocated memory Overview, how to find information about an
allocated memory block given an arbitrary pointer pointing
inside it and how to write simple memory-leak detection code.</P>
<A NAME="Removing"></A>
<H3>7.1 Removing allocations from the Allocated memory Overview</H3>
<P>Sometimes a program can allocate a very large number of memory blocks.
Having all of those in the Allocated memory Overview could make it
impractically large. Therefore it is possible to remove items from this list.</P>
<P>In the following example we make one allocation invisible by
using the function <CODE>make_invisible()</CODE>:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.1.cc -lcwd -o invisible</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.1.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
int* first = new int;
AllocTag2(first, "first");
int* second = new int;
AllocTag2(second, "second");
Debug( list_allocations_on(libcw_do) );
Debug( <SPAN class="highlight">make_invisible(</SPAN>first<SPAN class="highlight">)</SPAN> );
Debug( list_allocations_on(libcw_do) );
delete second;
delete first;
return 0;
}
<!-- END CODE examples7/test7.1.1.cc -->
</PRE>
<P>The output of this program is</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.1.1-bin -->
</PRE>
<P>As you can see, the first allocation at line 9 disappeared from the
overview after it was made invisible.</P>
<P>Pointer validation at de-allocation is still performed however.
For instance, when we make a mistake when freeing the first <CODE>int</CODE>:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.2.cc -lcwd -o coredump</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.2.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
int* first = new int;
AllocTag2(first, "first");
int* second = new int;
AllocTag2(second, "second");
Debug( list_allocations_on(libcw_do) );
Debug( make_invisible(first) );
Debug( list_allocations_on(libcw_do) );
delete second;
delete <SPAN class="highlight">[]</SPAN> first; // Make a deliberate error
return 0;
}
<!-- END CODE examples7/test7.1.2.cc -->
</PRE>
<P>then the output becomes</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.1.2-bin -->
</PRE>
<P>Also the function <CODE>test_delete()</CODE> still works:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.3.cc -lcwd -o test_delete</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.3.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.3.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
Debug( dc::notice.on() );
void* p = malloc(3000);
Debug( make_invisible(p) );
Debug( list_allocations_on(libcw_do) );
Dout(dc::notice, "test_delete(" << p << ") = " << <SPAN class="highlight">test_delete(</SPAN>p<SPAN class="highlight">)</SPAN>);
free(p);
Dout(dc::notice, "test_delete(" << p << ") = " << <SPAN class="highlight">test_delete(</SPAN>p<SPAN class="highlight">)</SPAN>);
return 0;
}
<!-- END CODE examples7/test7.1.3.cc -->
</PRE>
<P>results in</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.1.3-bin -->
</PRE>
<P>However, <CODE>find_alloc()</CODE>, the function that is explained in the next paragraph,
will <EM>fail</EM> to find an «invisible» block (it will return <CODE>NULL</CODE>).</P>
<A NAME="Retrieving"></A>
<H3>7.2 Retrieving information about memory allocations</H3>
<P>Libcwd allows the developer to generate powerful debugging output; aside from being able to test
if a given pointer points to the start of an allocated memory block, using <CODE>test_delete()</CODE>,
it is even possible to find all information about an allocated memory block that is also shown in the Allocated memory Overview
when given a pointer pointing anywhere <EM>inside</EM> an allocated memory block. For example:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.2.1.cc -lcwd -o find_alloc</CODE></P>
<P class="download">[<A HREF="examples7/test7.2.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.2.1.cc -->
#include "sys.h"
#include "debug.h"
using namespace libcwd;
template<typename T1, typename T2>
struct Foo {
T1 for_me;
T2 for_you;
double d;
};
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
Debug( dc::notice.on() );
Foo<char, int>* f = new Foo<char, int>;
AllocTag(f, "Our test object");
int* p = &f->for_you; // Pointer that points inside `f'
Dout(dc::notice, "f == " << static_cast<void*>(f));
Dout(dc::notice, "p == " << static_cast<void*>(p));
#ifdef CWDEBUG
alloc_ct const* alloc = <SPAN class="highlight">find_alloc(</SPAN>p<SPAN class="highlight">)</SPAN>;
Dout(dc::notice,
"p points inside \""
<< <SPAN class="highlight">alloc->description()</SPAN>
<< "\" starting at "
<< <SPAN class="highlight">alloc->start()</SPAN>
<< " with size "
<< <SPAN class="highlight">alloc->size()</SPAN>
<< '.');
Dout(dc::notice,
"This memory block contains a \""
<< <SPAN class="highlight">alloc->type_info().demangled_name()</SPAN> << "\".");
Dout(dc::notice,
"The allocation type is `"
<< <SPAN class="highlight">alloc->memblk_type()</SPAN>
<< "' and was allocated at "
<< <SPAN class="highlight">alloc->location()</SPAN>
<< '.');
#endif
return 0;
}
<!-- END CODE examples7/test7.2.1.cc -->
</PRE>
<P>The output of this program is</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.2.1-bin -->
</PRE>
<P>Note that the <EM>type</EM> returned by <CODE>alloc->type_info().demangled_name()</CODE> is
the type of the pointer passed to <CODE>AllocTag()</CODE>:
This string will always end on a <CODE>'*'</CODE>. </P>
<P>The original reason for supporting pointers that point <EM>inside</EM> a memory block
and not just to the start of it, was so that it could be used in base classes of objects
derived with multiple inheritance: <CODE>find_alloc(this)</CODE> will
always return the correct memory block, even if there is an offset between <CODE>this</CODE>
and the real start of the allocated block.
The same holds for arrays of objects allocated with <CODE>new[]</CODE>.</P>
<P>It is also possible to get the values for the number of bytes and blocks allocated
in total, as is printed at the top of each Allocated memory Overview.
See the next paragraph.</P>
<A NAME="Memory"></A>
<H3>7.3 Memory leak detection</H3>
<P><CODE>mem_blocks()</CODE> and <CODE>mem_size()</CODE>
(like everything else defined in namespace <CODE>libcwd</CODE>)
can be used to write a very simply memory leak detection system:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.1.cc -lcwd -o total_alloc</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.1.cc -->
#include "sys.h"
#include "debug.h"
using namespace libcwd;
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
char* memory_leak = new char [300];
AllocTag(memory_leak, "memory_leak");
// Debug( make_invisible(memory_leak) );
#ifdef CWDEBUG
if (<SPAN class="highlight">mem_blocks()</SPAN> > 0)
Dout(dc::warning, "There are still " << <SPAN class="highlight">mem_size()</SPAN> << " bytes allocated!");
else
Dout(dc::malloc, "No memory leaks.");
#endif
return 0;
}
<!-- END CODE examples7/test7.3.1.cc -->
</PRE>
<P>The output of this program is:</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.3.1-bin -->
</PRE>
<P>Invisible blocks are <B>not</B> detected!
When you comment out the <CODE>Debug( make_invisible(memory_leak) );</CODE> then
the output will read</P>
<PRE class="output">
MALLOC : operator new[] (size = 300) = 0x804fc88
MALLOC : No memory leaks.
</PRE>
<P>This allows you to improve the memory leak detection a bit in the case
of global objects that allocate memory. Nevertheless, that is bad
coding: you shouldn't define global objects that allocate memory.</P>
<P>Here is an example program anyway:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.2.cc -lcwd -o memleak</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.2.cc -->
#include "sys.h"
#include "debug.h"
class A {
private:
char* dynamic_memory;
public:
A(void)
{
dynamic_memory = new char [300];
AllocTag(dynamic_memory, "A::dynamic_memory");
}
~A()
{
if (dynamic_memory)
delete [] dynamic_memory;
}
};
// Global object that allocates memory (bad programming!)
A a;
int main(void)
{
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
#ifdef CWDEBUG
if (libcwd::mem_blocks() > 0)
{
Dout(dc::malloc|dc::warning, "Memory leak");
Debug( list_allocations_on(libcw_do) );
}
else
Dout(dc::malloc, "No memory leaks.");
#endif
return 0;
}
<!-- END CODE examples7/test7.3.2.cc -->
</PRE>
<P>results in</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.3.2-bin -->
</PRE>
<P>simply because <CODE>A::dynamic_memory</CODE> is not deleted until <EM>after</EM> <CODE>main</CODE>.</P>
<P>A simple kludge is to make all memory allocated <EM>before</EM> <CODE>main</CODE>, invisible:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.3.cc -lcwd -o memleak2</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.3.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.3.cc -->
#include "sys.h"
#include "debug.h"
class A {
private:
char* dynamic_memory;
public:
A(void)
{
dynamic_memory = new char [300];
AllocTag(dynamic_memory, "A::dynamic_memory");
}
~A()
{
if (dynamic_memory)
delete [] dynamic_memory;
}
};
// Global object that allocates memory (bad programming!)
A a;
int main(void)
{
<SPAN class="highlight">Debug( make_all_allocations_invisible_except(NULL) );</SPAN>
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
#ifdef CWDEBUG
if (libcwd::mem_blocks() > 0)
{
Dout(dc::malloc|dc::warning, "Memory leak");
Debug( list_allocations_on(libcw_do) );
}
else
Dout(dc::malloc, "No memory leaks.");
#endif
return 0;
}
<!-- END CODE examples7/test7.3.3.cc -->
</PRE>
<P>which will simply output</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.3.3-bin -->
</PRE>
<P>Libcwd provides an alternative way to check for memory leaks using so called «markers».
A marker is like a directory, any allocation made after a marker is created is put into that directory.
When a marker is removed and there are still allocation inside it, you will get a warning!
In the following example we allocate a memory block, then set a marker and next allocate two more memory
blocks.  The Allocated memory Overview is then printed.</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.4.cc -lcwd -o marker</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.4.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.4.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( make_all_allocations_invisible_except(NULL) );
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
int* p1 = new int [10];
AllocTag(p1, "p1");
<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
<SPAN class="highlight">libcwd::marker_ct* marker = new libcwd::marker_ct("A test marker");</SPAN>
<SPAN class="highlight">#endif</SPAN>
int* p2 = new int [20];
AllocTag(p2, "p2");
int* p3 = new int [30];
AllocTag(p3, "p3");
Debug( list_allocations_on(libcw_do) );
<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
// Delete the marker while there are still allocations inside it
<SPAN class="highlight">delete marker;</SPAN>
<SPAN class="highlight">#endif</SPAN>
return 0;
}
<!-- END CODE examples7/test7.3.4.cc -->
</PRE>
<P>The output of this program is:</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.3.4-bin -->
</PRE>
<P>Individual allocations (or other markers, inclusive everything they contain)
can be moved outside a marker with the function <CODE>move_outside()</CODE>.
For example, we could move the allocation of <CODE>p2</CODE> outside our marker:</P>
<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.5.cc -lcwd -o marker2</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.5.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.5.cc -->
#include "sys.h"
#include "debug.h"
int main(void)
{
Debug( make_all_allocations_invisible_except(NULL) );
Debug( libcw_do.on() );
Debug( dc::malloc.on() );
int* p1 = new int [10];
AllocTag(p1, "p1");
#if CWDEBUG_MARKER
libcwd::marker_ct* marker = new libcwd::marker_ct("A test marker");
#endif
int* p2 = new int [20];
AllocTag(p2, "p2");
int* p3 = new int [30];
AllocTag(p3, "p3");
<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
<SPAN class="highlight">Debug( move_outside(marker, p2) );</SPAN>
<SPAN class="highlight">#endif</SPAN>
Debug( list_allocations_on(libcw_do) );
#if CWDEBUG_MARKER
// Delete the marker while there are still allocations inside it
delete marker;
#endif
return 0;
}
<!-- END CODE examples7/test7.3.5.cc -->
</PRE>
<P>which results in the output</P>
<PRE class="output">
<!-- START OUTPUT examples7/test7.3.5-bin -->
</PRE>
__PAGEEND
<P class="line"><IMG width=870 height=19 src="../images/lines/hippo.png"></P>
<DIV class="buttons">
<A HREF="tut6.html"><IMG width=64 height=32 src="../images/buttons/lr_prev.png" border="0"></A>
<A HREF="index.html"><IMG width=64 height=32 src="../images/buttons/lr_index.png" border="0"></A>
<A HREF="tut8.html"><IMG width=64 height=32 src="../images/buttons/lr_next.png" border="0"></A>
</DIV>
__PAGEFOOTER
__HTMLFOOTER
|