2

so I have this class with a functions.hpp and a functions.cpp

#include "function.hpp"

test::test(int size)
{
    this->size = size;
    matrix = new int*[this->size];
    for (int i = 0; i < size; i++)
    {
        matrix[i] = new int[this->size];
    }
}

test::~test()
{
    for (int i = 0; i < this->size; i++)
        delete[] this->matrix[i];
    
    delete[] this->matrix;
}

but this still produces memory leaks, I am using the CRT library to test this

#include "function.hpp"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    int size = 10;
    test test(size);
    _CrtDumpMemoryLeaks();
    return 0;
}

this code still dumps memory leaks on me, and I've followed several tutorials and I'm starting to doubt either the CRT library or my computer.

3
  • 1
    Why are you not using vectors? Any specific reason? Or maybe,... even smart pointer will do. In a nutshell. avoid using <data-type>* in C++ Commented Aug 4, 2020 at 7:59
  • I did try to use vectors in my main program but I felt it cumbersome since I have some structs inside one of the arrays. Im thinking of going over to vectors but it still bothers me that this doesnt work. Commented Aug 4, 2020 at 8:01
  • 1
    "I felt it cumbersome" why? vector is more applicable to user defined types, because it doesn't default construct them all at once Commented Aug 4, 2020 at 8:07

1 Answer 1

6

The problem is that the destructor is not called until main is exited which is after you call _CrtDumpMemoryLeaks. Change your code to this

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    {
        int size = 10;
        test test(size);
    }
    _CrtDumpMemoryLeaks();
    return 0;
}

The extra {} ensure that the destructor is called before the call to _CrtDumpMemoryLeaks.

A different techique would be to write a class to perform the check.

class LeakCheck
{
public:
    LeakCheck() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); }
    ~LeakCheck() { _CrtDumpMemoryLeaks(); }
    LeakCheck(const LeakCheck&) = delete;
    LeakCheck& operator=(const LeakCheck&) = delete;
};

int main()
{
    LeakCheck _checker;
    int size = 10;
    test test(size);
    return 0;
}

Because destructors are called in reverse order of constructors, it's guaranteed that _CrtDumpMemoryLeaks will be called after the destructor of test.

I want to call LeakCheck a shim class but apparantly that's not the right terminology, maybe someone can tell me the correct term in the comments.

Sign up to request clarification or add additional context in comments.

1 Comment

I think the closest to the term you want, in terms of wide use, is "scope guard", though personally I don't find it to be the best name in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.