在单链接列表(与Dr.Memory)中识别内存泄漏有问题

时间:2019-02-05 22:54:04

标签: c++

我对内存分配和释放(删除)还很陌生,所以如果解决方案明确,我深表歉意。我有这个程序可以创建和处理链表(添加到前面,后面,插入,空闲等),当我运行Dr. Memory时,我遇到2条LEAK错误:

 ~Dr.M~~ Error #1: LEAK 16 direct bytes 0x00000000032614c0-0x00000000032614d0 + 0 indirect bytes
~~Dr.M~~ # 0 replace_operator_new                 [d:\drmemory_package\common\alloc_replace.c:2899]
~~Dr.M~~ # 1 msvcrt.dll!ftell                    +0x19e    (0x00007ff9c2dedaaf <msvcrt.dll+0x4daaf>)
~~Dr.M~~ # 2 msvcrt.dll!_iob_func                +0x51     (0x00007ff9c2ddcb22 <msvcrt.dll+0x3cb22>)
~~Dr.M~~ # 3 msvcrt.dll!fwrite                   +0x79     (0x00007ff9c2dedbaa <msvcrt.dll+0x4dbaa>)
~~Dr.M~~ # 4 libstdc++-6.dll!?                   +0x0      (0x000000006fcfb673 <libstdc++-6.dll+0xbb673>)
~~Dr.M~~ # 5 CS170::ListLab::Insert               [C:\Users\.../List.cpp:123]
~~Dr.M~~ # 6 libstdc++-6.dll!?                   +0x0      (0x000000006fcad5d2 <libstdc++-6.dll+0x6d5d2>)
~~Dr.M~~ # 7 KERNEL32.dll!BaseThreadInitThunk    +0x13     (0x00007ff9c2f03034 <KERNEL32.dll+0x13034>)
~~Dr.M~~
~~Dr.M~~ Error #2: LEAK 16 direct bytes 0x00000000032615b0-0x00000000032615c0 + 0 indirect bytes
~~Dr.M~~ # 0 replace_operator_new                    [d:\drmemory_package\common\alloc_replace.c:2899]
~~Dr.M~~ # 1 msvcrt.dll!write                       +0xb6     (0x00007ff9c2dbf717 <msvcrt.dll+0x1f717>)
~~Dr.M~~ # 2 msvcrt.dll!flsbuf                      +0x160    (0x00007ff9c2de6da1 <msvcrt.dll+0x46da1>)
~~Dr.M~~ # 3 libstdc++-6.dll!?                      +0x0      (0x000000006fcae568 <libstdc++-6.dll+0x6e568>)
~~Dr.M~~ # 4 libstdc++-6.dll!?                      +0x0      (0x000000006fcad5d2 <libstdc++-6.dll+0x6d5d2>)
~~Dr.M~~ # 5 CS170::ListLab::Insert                  [C:\Users\.../List.cpp:123]
~~Dr.M~~ # 6 CS170::ListLab::PrintList               [C:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/7.1.0/include/c++/ostream:613]
~~Dr.M~~ # 7 libstdc++-6.dll!?                      +0x0      (0x000000006fcad5d2 <libstdc++-6.dll+0x6d5d2>)
~~Dr.M~~ # 8 KERNEL32.dll!BaseThreadInitThunk       +0x13     (0x00007ff9c2f03034 <KERNEL32.dll+0x13034>)
~~Dr.M~~ Fetching 1 symbol files...
~~Dr.M~~ [1/1] Fetching symbols for C:\WINDOWS\System32\msvcrt.dll
~~Dr.M~~ Fetched 0 symbol files successfully

它指向此功能:

void Insert(Node **pList, int value, int position)
{
  struct Node *current = *pList;

  struct Node *newNode = MakeNode(value);

  int count = 0;

  if(position == 0)
  {
    *pList = newNode;
    (*pList)->next = current;

  }
  else
  {

    struct Node *previous = new Node; /*** this is line 123 ***/

    while(current->next != NULL && count != position)
    {
      if(count == (position - 1))
      {
        previous = current;
      }

      current = current->next;
      count++;
    }

    previous->next = newNode;
    newNode->next = current;
  }

  if(current->next == NULL && position == count+1)
  {
    newNode->next = NULL;

    while(current->next)
    {
      current = current->next;
    }

    current->next = newNode;
  }

  if(position > count + 1)
  {
    return;
  }
}

我认为释放前一个指针是一个问题,但是当我尝试删除它时,只会引起更多问题。我不确定如何解决此问题。再次致歉。谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

博士内存告诉您第123行有泄漏。您在代码中标记了第123行。在第123行,您正在分配新的Node。在您发布的代码中,您不会删除在第123行分配的内存。

在使用C ++时,要使用链表,您应该:

#include <list>
#include <memory>

...

std::list<std::unique_ptr<Node>> nodeList;
nodeList.push_back(std::make_unique<Node>());