20.4 Integrating Dmalloc
A huge number of bugs in C and C++ code are caused by mismanagement of memory. Using the wrapper functions described earlier (see section 220.127.116.11 Memory Management), or their equivalent, can help immensely in reducing the occurence of such bugs. Ultimately, you will introduce a difficult-to-diagnose memory bug inspite of these measures.
That is where Dmalloc(48) comes in. I recommend using it
routinely in all of your projects -- you will find all sorts of leaks
and bugs that might otherwise have lain dormant for some time. Automake
has explicit support for Dmalloc to make using it in your own projects
as painless as possible. The first step is to add the macro
`AM_WITH_DMALLOC' to `configure.in'. Citing this macro adds
a `--with-dmalloc' option to
The usefulness of Dmalloc is much increased by compiling an entire project with the header, `dmalloc.h' -- easily achieved in Sic by conditionally adding it to `common-h.in':
I have been careful to include the `dmalloc.h' header from the end
of this file so that it overrides my own definitions without
renaming the function prototypes. Similarly I must be careful to
accomodate Dmalloc's redefinition of the mallocation routines in
`sic/xmalloc.c' and `sic/xstrdup.c', by putting each file
inside an `#ifndef WITH_DMALLOC'. That way, when compiling the
project, if `--with-dmalloc' is specified and the
`WITH_DMALLOC' preprocessor symbol is defined, then Dmalloc's
debugging definitions of
Enabling Dmalloc is now simply a matter of reconfiguring the whole package using the `--with-dmalloc' option, and disabling it again is a matter of recofiguring without that option.
The use of Dmalloc is beyond the scope of this book, and is in any case described very well in the documentation that comes with the package. I strongly recommend you become familiar with it -- the time you invest here will pay dividends many times over in the time you save debugging.
This chapter completes the description of the Sic library project, and indeed tis part of the book. All of the infrastructure for building an advanced command line shell is in place now -- you need only add the builtin and syntax function definitions to create a complete shell of your own.
Each of the chapters in the next part of the book explores a more specialised application of the GNU Autotools, starting with a discussion of M4, a major part of the implementation of Autoconf.