遇到问题确定内存泄漏的单链表(带记忆博士)C ++

问题描述 投票:0回答:1

我非常新的内存分配和释放(删除),所以我很抱歉,如果该解决方案是明确的。我有这个计划,创建和操纵链表(添加到正面,背面,插入,免费等),当我运行内存博士,我得到2个泄漏错误:

 ~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;
  }
}

我认为这是与释放先前的指针有问题,但是当我尝试删除它,它只是造成更多的问题。我不知道如何解决这个问题。再次,我道歉,如果这是显而易见的。感谢您的帮助!

c++
1个回答
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>());
© www.soinside.com 2019 - 2024. All rights reserved.