Martol1ni
Martol1ni

Reputation: 4702

C++ delete [] - how to check if "all is deleted"?

I was wondering, throughout a program I am using a lot of char* pointers to cstrings, and other pointers. I want to make sure that I have delete all pointers after the program is done, even though Visual Studio and Code Blocks both do it for me (I think..).

Is there a way to check is all memory is cleared? That nothing is still 'using memory'?

Upvotes: 0

Views: 2562

Answers (6)

jcoder
jcoder

Reputation: 30035

If this is windows specific you can call this at the start of your program :-

_CrtSetDbgFlag(_crtDbgFlag | _CRTDBG_LEAK_CHECK_DF);

After including

#include <crtdbg.h>

And when your program exits the C++ runtime will output to the debugger a list of all memory blocks that are still allocated so you can see if you forgot to free anything.

Note that this only happens in DEBUG builds, in RELEASE builds the code does nothing (which is probabyl what you want anyway)

Upvotes: 0

phonetagger
phonetagger

Reputation: 7873

Unless you're writing driver code, there's absolutely nothing you can do with heap/freestore-allocated memory that would cause it to remain leaked once the program terminates. Leaked memory is only an issue during the lifetime of a given process; once a particular process has leaked its entire address space, it can't get any more for _that_particular_ process.

And it's not your compiler that does the uiltimate cleanup, it's the OS. In all modern operating systems that support segregated process address spaces (i.e. in which one process cannot read/write another process's address space, at least not without OS assistance), when a program terminates, the process's entire address space is reclaimed by the OS, whether or not the program has cleanly free()ed or deleted all of its heap/freestore-allocated memory. You can easily test this: Write a program that allocates 256 MBytes of space and then either exits or returns normally without freeing/deleting the allocated memory. Then run your program 16 times (or more). Shoot, run it 1000 times. If exiting without releasing that memory made it leak into oblivion until reboot, then you'd very soon run out of available memory. You'll find this to not be the case.

This same carelessness is not acceptable for certain other types of memory, however: If your program is holding onto any "handles" (a number that identifies some sort of resource granted to the process by the operating system), in some cases if you exit the program abnormally or uncleanly without releasing the handles, they remain lost until the next reboot. But unless your program is calling OS functions that give you such handles, that isn't a concern anyway.

Upvotes: 0

Attila
Attila

Reputation: 28762

You might want to use some sort of a smart pointer (see the Boost library, for example). The idea is that instead of having to manage memory manually (that is call delete explicitly when you don't need the object any more), you enlist the power of RAII to do the work for you.

The problem with manual memory management (and in general resource management) is that it is hard to write a program that properly deallocates all memory -- because you forget, or later when you change your code do not realize there was some other memory that needed to be deallocated.

RAII in C++ takes advantage of the fact that the destructor of a stack-allocated object is called automatically when that object goes out of scope. If the destructor logic is written properly, the last object that references (manages) the dynamically allocated data will be the one (and only one) that deallocates that memory. This could be achieved via reference counting, maintaining a list of references, etc.

The RAII paradigm for memory is in a sense similar to the garbage collection of mamged languages, except it is running when needed and dictated by your code, not at certain intervals, largely independent from your code.

Upvotes: 0

Mark Fraser
Mark Fraser

Reputation: 3198

Visual Studio is an IDE. It isn't even there by the time your code is deployed. It doesn't release memory for you.

For what you want you can look into tools like this:

http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page

Upvotes: 1

pg1989
pg1989

Reputation: 1010

The obvious answer on Linux would be valgrind, but the VS mention makes me think you're on Windows. Here is a SO thread discussing valgrind alternatives for windows.

Upvotes: 3

Ed Swangren
Ed Swangren

Reputation: 124692

I was wondering, throughout a program I am using a lot of char* pointers to cstrings

Why? I write C++ code every day and I very rarely use a pointer at all. Really it's only when using third party API's, and even then I can usually work around it to some degree. Not that pointers are inherently bad, but if you can avoid them, do so as it simplifies your program.

I want to make sure that I have delete all pointers after the program is done

This is a bit of a pointless exercise. The OS will do it for you when it cleans up your process.

Upvotes: 1

Related Questions