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:
- #define _CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include <crtdbg.h>
- #include <iostream>
- class Test
- {
- public:
- Test(int nRows, int nCols, const double* inputData)
- {
- row = nRows;
- col = nCols;
- num = row * col;
- data = new double[num];
- for (int i = 0; i < num; i++)
- data[i] = inputData[i];
- }
- ~Test()
- {
- delete[] data;
- std::cout << "Memory released!" << std::endl;
- }
- private:
- double* data;
- int row, col, num;
- };
- 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);
- _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
- _CrtDumpMemoryLeaks();
- return 0;
- }