- publishing free software manuals
Valgrind 3.3 - Advanced Debugging and Profiling for GNU/Linux applications
by J. Seward, N. Nethercote, J. Weidendorfer and the Valgrind Development Team
Paperback (6"x9"), 164 pages
ISBN 0954612051
RRP £12.95 ($19.95)

Get a printed copy>>>

5.3.4 When a block is freed with an inappropriate deallocation function

In the following example, a block allocated with new[] has wrongly been deallocated with free:

Mismatched free() / delete / delete []
   at 0x40043249: free (vg_clientfuncs.c:171)
   by 0x4102BB4E: QGArray::~QGArray(void)
                    (tools/qgarray.cpp:149)
   by 0x4C261C41: PptDoc::~PptDoc(void)
                    (include/qmemarray.h:60)
   by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
 Address 0x4BB292A8 is 0 bytes
   inside a block of size 64 alloc'd
   at 0x4004318C: operator new[](unsigned int) 
                    (vg_clientfuncs.c:152)
   by 0x4C21BC15: KLaola::readSBStream(int)
                    const (klaola.cc:314)
   by 0x4C21C155: KLaola::stream(KLaola::OLENode
                    const *) (klaola.cc:416)
   by 0x4C21788F: OLEFilter::convert(QCString
                    const &) (olefilter.cc:272)

In ‘C++’ it's important to deallocate memory in a way compatible with how it was allocated. The deal is:

The worst thing is that on Linux apparently it doesn't matter if you do mix these up, but the same program may then crash on a different platform, Solaris for example. So it's best to fix it properly. According to the KDE folks “it's amazing how many C++ programmers don't know this”.

The reason behind the requirement is as follows. In some C++ implementations, delete[] must be used for objects allocated by new[] because the compiler stores the size of the array and the pointer-to-member to the destructor of the array's content just before the pointer actually returned. This implies a variable-sized overhead in what's returned by new or new[].

ISBN 0954612051Valgrind 3.3 - Advanced Debugging and Profiling for GNU/Linux applicationsSee the print edition