C++ 链表内存泄漏

C++ 链表内存泄漏,c++,memory-leaks,linked-list,C++,Memory Leaks,Linked List,我正在浏览一个链表并试图删除分配的内存,但是我看到很多内存泄漏。我真的不知道为什么。相关代码: Winery类的构造函数和析构函数: Winery::Winery(const char * const name, const char * const location, const int acres, const int rating) : name(new char[strlen(name) + 1]), location(new char[strlen(location) + 1]), a

我正在浏览一个链表并试图删除分配的内存,但是我看到很多内存泄漏。我真的不知道为什么。相关代码:

Winery类的构造函数和析构函数:

Winery::Winery(const char * const name, const char * const location, const int acres, const int rating) :
name(new char[strlen(name) + 1]),
location(new char[strlen(location) + 1]),
acres(acres),
rating(rating)
{
    strcpy(this->name, name);
    strcpy(this->location, location);
}

Winery::~Winery()
{
    delete[] this->name;
    delete[] this->location;
    this->name = nullptr;
    this->location = nullptr;
}
my List类的构造函数和析构函数:

List::List() :
headByName(nullptr),
headByRating(nullptr)
{

}

List::~List()
{
    Node    *frontNode(headByName);
    Node    *nextNode;

    while (frontNode) 
    {
        nextNode = frontNode->nextByName;
        delete frontNode;
        frontNode = nextNode;
    }
}
List::Node::Node(const Winery& winery) :
item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating()),
nextByName(nullptr),
nextByRating(nullptr)
{

}
列表类中包含的节点的构造函数:

List::List() :
headByName(nullptr),
headByRating(nullptr)
{

}

List::~List()
{
    Node    *frontNode(headByName);
    Node    *nextNode;

    while (frontNode) 
    {
        nextNode = frontNode->nextByName;
        delete frontNode;
        frontNode = nextNode;
    }
}
List::Node::Node(const Winery& winery) :
item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating()),
nextByName(nullptr),
nextByRating(nullptr)
{

}
最后,我使用一个函数将节点插入链表。这是我动态分配内存的唯一其他地方,所以它必须在这里的某个地方,但我不知道它可能在哪里

void List::insert(const Winery& winery)
{
    if (!headByName)
    {
        headByName = new Node(winery);
    }
    else
    {
        Node    *frontNode(headByName), *prevNode(headByName);
        Node    *newerNode = new Node(winery);

        frontNode = headByName;
        prevNode = frontNode;
        while (frontNode->nextByName)
        {
            if (strcmp(frontNode->item.getName(), winery.getName()) < 0)
            {
                frontNode->nextByName = prevNode->nextByName;
                prevNode->nextByName = newerNode;
            }
            else
            {
                prevNode = frontNode;
                frontNode = frontNode->nextByName;
            }
        }

        frontNode = headByName;
        prevNode = frontNode;
        while (frontNode->nextByName)
        {
            if (frontNode->item.getRating() < winery.getRating())
            {
                frontNode->nextByRating = prevNode->nextByRating;
                prevNode->nextByRating = newerNode;
            }
            else
            {
                prevNode = frontNode;
                frontNode = frontNode->nextByRating;
            }
        }
    }
}
void List::insert(const Winery&Winery)
{
如果(!headByName)
{
headByName=新节点(酿酒厂);
}
其他的
{
节点*frontNode(headByName),*prevNode(headByName);
Node*newerNode=新节点(酿酒厂);
frontNode=headByName;
prevNode=frontNode;
while(frontNode->nextByName)
{
if(strcmp(frontNode->item.getName(),winery.getName())<0)
{
frontNode->nextByName=prevNode->nextByName;
prevNode->nextByName=newerNode;
}
其他的
{
prevNode=frontNode;
frontNode=frontNode->nextByName;
}
}
frontNode=headByName;
prevNode=frontNode;
while(frontNode->nextByName)
{
if(frontNode->item.getRating()nextByRating=prevNode->nextByRating;
prevNode->nextByRating=newerNode;
}
其他的
{
prevNode=frontNode;
frontNode=frontNode->nextByRating;
}
}
}
}
我能想到的唯一一件事是,一定有一些复制品在某个地方,然后就留在那里,但我不知道可能在哪里。我的析构函数看起来不错,所以我不知道我可能在哪里泄漏内存

编辑:我被要求展示酒厂课程的全部内容。以下是头文件:

#include <ostream>

class Winery
{
public:

    Winery(const char * const name, const char * const location, const int acres, const int rating);
    virtual ~Winery(void);

    const char * const getName() const { return name; }
    const char * const getLocation() const { return location; }
    const int getAcres() const { return acres; }
    const int getRating() const { return rating; }

    static void displayColumnHeadings(std::ostream& out);

    friend std::ostream& operator<<(std::ostream& out, Winery *w);

private:
    char    *name;
    char    *location;
    int     acres;
    int     rating;
};
#包括
高级酿酒厂
{
公众:
酿酒厂(const char*const name、const char*const location、const int acres、const int rating);
虚拟酿酒厂(void);
const char*const getName()const{return name;}
const char*const getLocation()const{return location;}
const int getAcres()const{return acres;}
const int getRating()const{return rating;}
静态无效显示列标题(标准::ostream&out);

friend std::ostream和操作员>>移除库珀山“是什么指示您存在内存泄漏?您是否有应用程序终止时生成的某种报告?不确定为什么在对象中使用
this
指针。您不需要这样做。还有那些使用
strcpy()的构造函数。”
函数确实应该检查指针是否为
nullptr
。是的,我正在使用教授提供的一个额外文件来检测内存泄漏。如果有帮助的话,我正在使用Visual Studio 2013。因此,在Visual Studio中运行调试构建应该会为您提供输出,说明在哪里为任何内存分配了内存应用程序终止时留下的。该报告怎么说?@RichardChambers-像这样?
if(name){name=nullptr}