Thread: Looking for a solution to a memory leak with pointers in a class

  1. #1
    Registered User
    Join Date
    Jan 2023
    Posts
    4

    Looking for a solution to a memory leak with pointers in a class

    Although the data is deleted, MSVC shows that the following code has a memory leak issue. Does anyone know how to fix the memory leak bug in the class? Thanks.

    Code:
     
    
    
    1. #define _CRTDBG_MAP_ALLOC
    2. #include <stdlib.h>
    3. #include <crtdbg.h>
    4. #include <iostream>
    5. class Test
    6. {
    7. public:
    8. Test(int nRows, int nCols, const double* inputData)
    9. {
    10. row = nRows;
    11. col = nCols;
    12. num = row * col;
    13. data = new double[num];
    14. for (int i = 0; i < num; i++)
    15. data[i] = inputData[i];
    16. }
    17. ~Test()
    18. {
    19. delete[] data;
    20. std::cout << "Memory released!" << std::endl;
    21. }
    22. private:
    23. double* data;
    24. int row, col, num;
    25. };
    26. int main()
    27. {
    28. double inputData[12] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 };
    29. Test test1(3, 4, inputData);
    30. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
    31. _CrtDumpMemoryLeaks();
    32. return 0;
    33. }
    Last edited by happyCodeBird; 01-18-2023 at 08:01 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    It's pretty clear that your program is not leaking memory!
    However, at the point that you call those debug functions, the object's destructor has yet to be called, so the memory is still allocated.
    Try this version:
    Code:
    #define _CRTDBG_MAP_ALLOC
    #include <iostream>
    #include <crtdbg.h>
     
    class Test
    {
        int row, col;
        double* data;
    public:
        Test(int nRows, int nCols, const double* inputData)
            : row(nRows), col(nCols), data(new double[row * col])
        {
            for (int i = 0; i < row * col; i++)
                data[i] = inputData[i];
        }
        ~Test()
        {
            delete[] data;
        }
        int rows() const { return row; }
        int cols() const { return col; }
    };
     
    void f(const double *data) {
        Test t(3, 4, data);
        std::cout << t.rows() << ',' << t.cols() << '\n';
    } // t is destructed (and its memory freed) just before f() returns
     
    int main()
    {
        double data[12] = {
             1.0,  2.0,  3.0,  4.0,
             5.0,  6.0,  7.0,  8.0,
             9.0, 10.0, 11.0, 12.0
        };
        f(data);    
        _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
        _CrtDumpMemoryLeaks();
        return 0;
    }
    Alternatively, you might try not calling those functions and instead adding this as the first call in main:
    Code:
    _CrtSetDebugFlag(_CRTDBG_LEAK_CHECK_DF);
    That might not give you the false positive in your original code.

    Also look here:
    Find memory leaks with the CRT Library - Visual Studio (Windows) | Microsoft Learn
    Last edited by john.c; 01-18-2023 at 08:32 PM.
    All truths are half-truths. - A.N. Whitehead

  3. #3
    Registered User
    Join Date
    Jan 2023
    Posts
    4
    Thanks very much for pointing out the way MSVC debug works!

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    Note that I added a little to my original post.
    All truths are half-truths. - A.N. Whitehead

  5. #5
    Registered User
    Join Date
    Jan 2023
    Posts
    4
    Yes. It is very helpful.

    By the way, I think the function should be:
    Code:
    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    You can get the same effect as john.c's post with just a pair of braces.
    Code:
    int main()
    {
        double inputData[12] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 };
        {
            Test test1(3, 4, inputData);
        } // now Test dtor is called here
        _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
        _CrtDumpMemoryLeaks();
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2023
    Posts
    4
    It also works.
    Thanks for the solution!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By *nick in forum Windows Programming
    Replies: 8
    Last Post: 09-17-2012, 06:15 PM
  2. Memory Leak!!!!
    By subhashish1213 in forum C++ Programming
    Replies: 10
    Last Post: 09-14-2009, 12:01 AM
  3. Memory Leak
    By P4R4N01D in forum Windows Programming
    Replies: 22
    Last Post: 06-12-2008, 01:11 AM
  4. Singleton leak. Let us finish this (aka Partial Solution)
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 09:42 AM
  5. Replies: 2
    Last Post: 09-28-2006, 01:06 PM

Tags for this Thread