Re: debugging memory leaks

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Bonj (benjtaylor)
Date: 12/12/04


Date: Sun, 12 Dec 2004 16:13:00 -0000


> ...
> #include <malloc.h>
>
> #ifdef _DEBUG
> LPVOID operator myNew[] (size_t blocksize, const char *file, int line)
> {
> LPVOID ret = malloc(blocksize);
> TRACE(_T("*** Allocated memory: 0x%x, in file: \"%s\" at line %d"),
> (ULONG)ret, file, line);
> return ret;
> }

That's fine...

> void operator myDelete[] (LPVOID block, const char* file, int line)
> {
> TRACE(_T("*** Freed memory: 0x%x, in file: \"%s\" at line %d"),
> (ULONG)block, file, line);
> free(block);
> }
>

Yes...

>
> #define new(block) myNew(block, __FILE__, __LINE__)
> #define delete(block) myDelete(block, __FILE_, __LINE__)
>
> // else, when in Released mode - using default 'new' and 'delete'
> operators
> #endif
>
> int main()
> {
> char* c = new char[255];
> delete c;

I hate to criticise asy ou are so well meaning, but surely isn't that the
most glaring memory leak bug ever? new[] should be matched with delete[],
you've matched new[] with delete. (without the [].)
That allocates an entire array, but only deletes the first element of it -
and I'm unlikely to find that out since I've overloaded it with a function
that tells me that I've deleted that block.
The distinction between delete and delete[] in C++ came about because delete
can be very much faster if it can know that it only has to free one
element - when something is allocated with new, delete knows how big it is
because you are deleting a pointer to a certain type, so if you do
wchar_t w = new wchar_t;
delete w; //which is a wchar_t, hence delete knows to delete 2 bytes
But if you new[] something, it has to store the number of elements
somewhere - either in a lookup table or somewhere within the memory
allocated, BSTR-style. Apparently both of these are acceptable methods in
commercial grade compilers, but it essentially means that delete[] has to
lookup this information, so to prevent the normal delete having to do the
lookup aswell, you put square brackets on to tell it to look upthe
information as to how many elements there are.

I hope you haven't got lots of code like that that you've now got to
modify...:-)

I actually tend to use something like
#define NULLSAFEDELETE(x) {if(x != NULL) {delete x; x = NULL;}}
#define NULLSAFEDELETE_ARR(x) {if(x != NULL) {delete[] x; x = NULL;}}

That way, I can call it on anything, and I don't have to worry about
accidentally calling it on a NULL pointer. It also had the unforeseen
advantage that I could modify all my calls to delete([]) in all one place...
but like Victor says I don't actually need to know in what functions
delete([]) was called, I just need to know what blocks were freed- I can
then deduce what (if any) blocks were new'ed and not deleted, but since I do
know where the new occurred, I can work out where the delete *should* have
occurred.

> Then view the 'Output'-pane of VisualStudio

If I used VisualStudio, I'd not have the pleasure of this challenge - I'd
just fall back on the use of _CrtDumpMemoryLeaks. Although even that doesn't
tell me about SysFreeString/SysAllocString bugs.



Relevant Pages

  • Re: Gove in a virtual world
    ... Then you'll know if it is braindead (allocates, frees) or intelligent which may be important if you think you have a pointer to a block of memory to know if it has moved or not. ... [but, then, I never felt GOTO was inherently evil, it's just a JMP by another name; the evil is in dialects of programming languages where GOTO are the *only* tools you have for branching to different parts of the program] ... How about we think of what is going to be the best for learning the principles of programming, instead of "what is used in schools"? ...
    (comp.sys.acorn.misc)
  • Re: Small Variant Records in C
    ... 2.4GB of character data and 15mins for a self-compile. ... I used two bits to distinguish a pointer to a heap descriptor from ... So on a system with 32 bit pointers, a character is 32 bits wide, and not heap ... The same size simplifies the garbage collector which allocates heaps ...
    (comp.lang.c)
  • Re: "Mastering C Pointers"....
    ... > If all the structs are part of a single object (e.g., ... For each pointer, cast it to char* and subtract the ... > details of how mallocallocates memory. ...
    (comp.lang.c)
  • Re: compile
    ... which allocates memory on a garbage-collected heap. ... which identifies some pointer or GCMalloc'd structure as ... in compilation with dummy arguments so as to fake a call to a routine ...
    (comp.lang.lisp)
  • Re: Explications for malloc ang some questions about pointers
    ... A pointer is a thing that contains the address of some ... block of memory somewhere. ... "malloc" allocates a number of bytes and returns a pointer to ... You have to go through the lists a_tmp and b_tmp, ...
    (comp.lang.c)