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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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