Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尽管有析构函数调用,内存仍然泄漏_C++_Visual Studio 2012_Memory Leaks - Fatal编程技术网

C++ 尽管有析构函数调用,内存仍然泄漏

C++ 尽管有析构函数调用,内存仍然泄漏,c++,visual-studio-2012,memory-leaks,C++,Visual Studio 2012,Memory Leaks,我正在制作一个类LinkedList,如下所示: private: LL_element* first; LL_element* last; int size; public: //constructor Linkedlist(): first(NULL), last(NULL), size(0) {} //destructor ~Linkedlist(); //adds element at the end of the l

我正在制作一个类LinkedList,如下所示:

private:
    LL_element* first;
    LL_element* last; 
    int size;
public:
    //constructor
    Linkedlist(): first(NULL), last(NULL), size(0) {}

    //destructor
    ~Linkedlist();

    //adds element at the end of the list.
    void push_back(int value);

    //removes an element at the end of the list.
    bool pop_back(int& value);
int main(int argc, const char * argv[]) {
    {
        Linkedlist foo; 
        foo.push_back(3);
        foo.push_back(5);
        foo.push_back(6);
        foo.push_back(7); 
    }
    _CrtDumpMemoryLeaks();
    return 0;
}
函数push_back创建一个新的LL_元素对象(动态分配)。 函数pop_back显式删除列表末尾的元素。 只要列表不为空,析构函数就使用函数pop_back。 问题是,当我创建Linkedlist对象时:

Linkedlist foo =   Linkedlist();
foo.push_back(3);
foo.push_back(5);
当foo超出范围时,会调用析构函数,但Visualstudio仍然会为我提供LL_元素的记忆库。但是,当我动态分配:

Linkedlist* foo = new Linkedlist();
foo->push_back(3);
foo->push_back(5);
然后用“delete”调用析构函数,VS不提供memoryleaks

编译器是否未正确调用析构函数,或者在堆栈上创建Linkedlist时是否未正确使用默认构造函数?真的 让我困惑

push_back和pop_back的代码:

bool Linkedlist:: pop_back(int& value) {

//only if the list is not empty, an element can be removed.
if(!this->is_empty()) {

    value = this->last->get_value();
    LL_element* removed = this->last;
    this->last = removed->get_previous();

    if(this->size!=1) {
    this->last->set_next(NULL);
    }

    delete removed; 
    size--;
    return true;
}   

value = 0;
cout << EMPTY_LIST_MESSAGE << endl;
return false;}

void Linkedlist:: push_back(int value)
{

LL_element* to_add = new LL_element(value);

//if there already is a first element, we can ignore it
if(!this->is_empty()) {

    this->last->set_next(to_add);
    to_add->set_previous(last);
    this->last = to_add;
    size++;

}

//if the list is empty --> special case.
else {

    this->first = to_add;
    this->last = to_add;
    size++;

}
}
输出:

Detected memory leaks!
Dumping objects ->
{139} normal block at 0x00A58148, 12 bytes long.
 Data: <            > 00 00 00 00 00 81 A5 00 07 00 00 00 
{138} normal block at 0x00A58100, 12 bytes long.
 Data: <H           > 48 81 A5 00 B8 80 A5 00 06 00 00 00 
{137} normal block at 0x00A580B8, 12 bytes long.
 Data: <     0      > 00 81 A5 00 F0 30 A5 00 05 00 00 00 
{136} normal block at 0x00A530F0, 12 bytes long.
 Data: <            > B8 80 A5 00 00 00 00 00 03 00 00 00 
Object dump complete.
The program '[5592] linkedlist.exe' has exited with code 0 (0x0).
检测到内存泄漏!
转储对象->
0x00A58148处的{139}正常块,12字节长。
数据:<>00 00 81 A5 00 07 00 00
0x00A58100处的{138}正常块,12字节长。
资料:48 81 A5 00 B8 80 A5 00 06 00 00
0x00A580B8处的{137}正常块,12字节长。
数据:<0>0081 A500 F0 30 A500 05 00 00
0x00A530F0处的{136}正常块,12字节长。
数据:<>B8 80 A500 00 00
对象转储完成。
程序“[5592]linkedlist.exe”已退出,代码为0(0x0)。

在foo超出范围之前,您正在检查内存泄漏,因此它没有机会调用其析构函数,而析构函数可能会清除所有LL_元素(假设,因为您没有发布析构函数代码)。试着这样做:

private:
    LL_element* first;
    LL_element* last; 
    int size;
public:
    //constructor
    Linkedlist(): first(NULL), last(NULL), size(0) {}

    //destructor
    ~Linkedlist();

    //adds element at the end of the list.
    void push_back(int value);

    //removes an element at the end of the list.
    bool pop_back(int& value);
int main(int argc, const char * argv[]) {
    {
        Linkedlist foo; 
        foo.push_back(3);
        foo.push_back(5);
        foo.push_back(6);
        foo.push_back(7); 
    }
    _CrtDumpMemoryLeaks();
    return 0;
}

请发布一篇演示内存泄漏的文章。也许您可以向我们展示析构函数的代码并将其推回()?将在堆栈上正确调用构造函数和析构函数。当您使用
new
动态创建Linkedlist并且从不在任何地方调用相应的
delete
时,将根本不会调用析构函数。在任何情况下,问题都可能出现在您的
push_-back
pop_-back
或析构函数的实现中,因此也请发布此代码。虽然这可能与回答您的问题无关,但我强烈建议您查看
std::unique_-ptr
std::shared_ptr
等等。首先,必须学习基础知识,但同样重要的是,在现代C++中,手动资源(内存)管理(调用新的和删除)不是做事的方式。还可以看看“RAII”。我已经了解了独特的和共享的ptr。但是,我不允许在考试中使用这些,因为考试是在一个较老的C++版本中,而不是C++ 11.谢谢!在析构函数中,我调用函数pop_直到大小为零,我认为这是一种正确的方法。