Vineel Kumar Reddy
Vineel Kumar Reddy

Reputation: 4726

Memory leak detection in programs developing on windows

I am working in visual studio to develop a program in C. Is there a way in which I can detect memory leaks of my program in visual studio? Or in general any memory leak detection library for windows developer(similar to valgrind for linux).... Please let me know. Thanks.

Upvotes: 2

Views: 514

Answers (5)

Well I know this topic is old, but I achieved detecting my memory leaks by Improving actual malloc and free functions with my own functions. Sadly you need to allocate and free memories using these functions and this solution works Windows only.

If you are interested in this then just Include this inside your code or make a header for this.

#include <malloc.h>

typedef struct{
    BOOL detection;
    LONG allocs;
    LONG frees;
    LONG memoryUsed;
    LONG memoryReleased;
    LONG memoryActual;
} MEMORY_WATCH;
MEMORY_WATCH MEMORY = {FALSE, 0, 0, 0, 0, 0};
/**
 * Controlled Memory Allocations.
 * 
 * @param size Size of the requested memory.
 * @return A pointer to requested memory.
 */
void *CMalloc(size_t size){
    void *memblock = malloc(size);
    if(MEMORY.detection && memblock != NULL){
        MEMORY.allocs++;
        MEMORY.memoryUsed += size;
        MEMORY.memoryActual += size;
    }
    return memblock;
}
/**
 * Controlled Memory Release.
 * 
 * @param memblock A pointer to memory that is going to be released.
 */
void CFree(void *memblock){
    if(MEMORY.detection && memblock != NULL){
        MEMORY.frees++;
        MEMORY.memoryReleased += _msize(memblock);
        MEMORY.memoryActual -= _msize(memblock);
    }
    free(memblock);
}

On the start of the Main program type MEMORY.detection = TRUE and on the end of the main you can output it like this:

printf("\n\nMemory result:\n");
printf("\t%ld calls\tAllocated\n\t%ld calls\tFreed\n\n", MEMORY.allocs, MEMORY.frees);
printf("\t%ld bytes\tUsed\n\t%ld bytes\tReleased\n\n", MEMORY.memoryUsed, MEMORY.memoryReleased);
printf("\t%ld bytes\tMissing\n", MEMORY.memoryActual);

I applied this solution into my program and I found a leak that took me 7 bytes. Just forgot to free one memory block.

Number of Values: 7
        Value(4): Path
                (132):  REG_EXPAND_SZ
                "C:\ProgramFiles\Windows\System Overflow.exe" "-i -x -v file.txt"
        Value(9): Languages
                (6):    REG_MULTI_SZ
                (6): EN
                (6): SK
                (6): CZ
                (8): GER
                (6): JP
                (6): RU
        Value(16): SelectedLanguage
                (6):    REG_SZ
                EN
        Value(11): KeyModifier
                (6):    REG_BINARY
                0 16 17 33 99 113
        Value(9): SpecialID
                (4):    REG_DWORD
                22689
        Value(8): UniqueID
                (8):    REG_QWORD
                110022689
        Value(9): Standards
                (5):    REG_MULTI_SZ
                (14): ISO600
                (16): ISO9236
                (18): ISO9236a
                (18): ISO9236b
                (14): ISO512


Memory result:
        34 calls        Allocated
        33 calls        Freed

        374 bytes       Used
        367 bytes       Released

        7 bytes Missing

RUN SUCCESSFUL (total time: 22ms)

Upvotes: 0

Jan S
Jan S

Reputation: 1837

I use Visual Leak Detector . It's free and very effective, and you only have to include the header file <vld.h> in your program.
It gives the complete stack trace of the memory that has been allocated and not freed.

Upvotes: 1

MastAvalons
MastAvalons

Reputation: 1141

For work in Visual Studio has already developed and reliable plugins. As for me, I like deleaker, it is easy to use.

Upvotes: 0

Marlon
Marlon

Reputation: 20332

You could #include <crtdbg.h>

At the start of your program enter the following code:

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEMDF | _CRT_LEAK_CHECK_DF);

or at the end of your program before you return an exit code:

_CrtDumpMemoryLeaks();

The first way is better imo, because it will automatically display memory leaks any time the program exits and it is only one line of code. If you use _CrtDumpMemoryLeaks(), you are forced to place it everywhere your program could potentially exit.

Any memory leaks will be displayed in your output window when the program exits. This only works in visual studio.

I don't think this will show the file and line number of where the allocation took place though. In C++ you can if you redefine the new keyword to display where the allocation took place:

#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

Upvotes: 2

Faken
Faken

Reputation: 11822

I'm no expert programmer but I used the Intel parallel studio to check for memory leaks. Its pretty incredible, integrates into visual studio seamlessly and provides extremely simple controls to detect all sorts of errors in your program. Pretty much just install and run it to start finding memory errors.

Only problem was the price tag though there is a 30 day trial.

Upvotes: 0

Related Questions