简体   繁体   English

需要帮助寻找内存泄漏

[英]Need help finding memory leak

In my CST class we are learning about memory on the heap in C++. 在我的CST课程中,我们正在学习C ++中堆上的内存。 Keep in mind this is how the code should be written (must use no std::vector or similar helpful classes). 请记住,这就是编写代码的方式(一定不要使用std :: vector或类似的有用类)。 Below is the code I wrote to my assignment and I tested it for memory leaks through Visual Studio and is returns: 下面是我写给作业的代码,并通过Visual Studio测试了它的内存泄漏,并返回:

{173} normal block at 0x008BF658, 257 bytes long.
Data: <                > 00 CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 

which is this line : names[i] = new char[257]; 这是这一行: names[i] = new char[257];

but later on I delete it with : delete [] names[i]; 但后来我用以下命令将其删除: delete [] names[i];

Don't know why this isn't working. 不知道为什么这不起作用。 Here is my code consisting of a simple recursive function and the main: 这是由简单的递归函数和主要代码组成的代码:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include "CST126SRS02.h"

#define _CRTDBG_MAP_ALLOC  
#include <stdlib.h>  
#include <crtdbg.h>  


using namespace std;


void reverseNames(int &numNames, char ** &names, char ** &temp)
{
    //get names from user, put names in ragged array
    names = new char * [numNames] {};
    int i = 0;
    while (i < numNames) {
        //add name to ragged array
        if (cin.peek() == '\n' && i != 0) break; //make sure there are names
        if (!cin.good()) return; //end of file or errors 

        names[i] = new char[257];
        cin >> setw(256) >> names[i];
        i++;

        if (i == numNames) {
            //need a bigger array, reallocation
            numNames *= 2;

            temp = new char *[numNames] {};
            for (int j = 0; j < numNames / 2; j++)
            {
                temp[j] = names[j];
            }

            delete[] names;
            names = temp;
        }
    }

    //output names in reverse order
    for (int i = numNames - 1; i >= 0; i--) {
        if (names[i] != nullptr) cout << names[i] << " ";
        delete [] names[i]; //also deletes temp items!
    }
    delete [] names; //also deletes temp array!
    cout << endl;

    //recursion
    numNames = 1;
    reverseNames(numNames, names, temp);
}

int main()
{
    //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    //_CrtSetBreakAlloc(173);

    int numNames = 1;
    char **names;
    char ** temp;

    //recursive function, runs until user ends file
    reverseNames(numNames, names, temp);

    delete [] names;

    _CrtDumpMemoryLeaks();

    return 0;
}

Where is the memory leak? 内存泄漏在哪里? Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

Memory Leak Detection This line will output memory leak information to output window. 内存泄漏检测此行会将内存泄漏信息输出到输出窗口。

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

Take the value in the paranthesis and insert into this line below (183, 184, 150). 取括号中的值,并插入下面的行(183,184,150)。 The application will then break at the allocation of the memory leak which you can trace back through the call stack. 然后,应用程序将在分配内存泄漏时中断,您可以通过调用堆栈进行追溯。

_CrtSetBreakAlloc(Your value);

Adding info specifically for this thread: 专门为此线程添加信息:

char** array = new char*[size]; //This is an array of pointers.
for(int i=0; i<size;i++) { array[i] = new char(); } // Each element in the array creates an object.

To delete: 删除:

for(int i=0; i<size; i++){ delete array[i]; } //delete the objects.
delete[] array; //delete the array.

If this if happens names variable will leak. 如果发生这种情况, names变量将泄漏。

if (!cin.good()) return; //end of file or errors 

To be precise in this case new char[257]; 在这种情况下,确切地说是new char[257]; will not be deallocated on return. 退货时不会被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM