解决与链表c ++相关的内存泄漏问题

时间:2014-10-07 02:30:59

标签: c++ visual-c++ memory-leaks

我有一个链表,用于处理插入和删除链接列表中的指针,这是我自己实现的。使用内存泄漏查找器类,我继续在一行和一行上得到内存泄漏....这在代码中是BOLDED。具体如下:

char *name;
char phone[ PHONE_LEN ];

链接列表的删除()方法


bool LList::Delete ( const InfoType & x )
{
   Node *p = list;
   Node *q = NULL;

   while(p->infoPtr != NULL && *(p->infoPtr) != x) //While not on node to delete
   {
      q = p;
      p = p->next;
   }
   if(p == NULL) //Empty or End
      return false;
   else if(q == NULL)//First node
   {
      list = p->next;
      delete p;
      return true;
   }
   else //middle node
   {
      q->next = p->next;
      delete p; // <- This is where I expect the delete to be taken care of
      return true;
   }
}

我的运营商超载直接公司公司对象

std::istream& operator>>(std::istream& in, Company& m)
{
   char temp[MAX_NAME_LEN + 1];

   in >> temp;
   int length = strlen(temp);
   m.name = new char[length + 1]; **//This line is the one where memory leak is occurring**
   strcpy(m.name, temp);

   for(int i = 0; i < PHONE_LEN; i++)
      in >> m.phone[i];

   return in;
}

析构函数是否重要

Company::~Company() 
{
   if(name != NULL)
      delete [] name;
}

我的问题是:我应该如何删除在Linked List类中我的delete方法里面指出的行中创建的对象?我将编辑并发布有关我的程序的任何其他帮助。

编辑:

struct Node
   {
      Node ( InfoType * x, Node * p = NULL ) { infoPtr = x;  next = p; }
      ~Node() { delete infoPtr; }
      InfoType * infoPtr;
      Node * next;
   };

   Node * list;

0 个答案:

没有答案