c++ - 指针和整数模板的内存泄漏。我究竟做错了什么?

标签 c++ templates memory-leaks valgrind

了解 C++,并尝试将类模板化以使用结构的 Int 和指针。 输出符合预期,但使用 valgrind 进行测试时,似乎有内存泄漏,来自未释放的内存。

我相信这与我在类 init 中声明列表变量的方式有关。

我错过了什么,我该如何解决? 谢谢。

#include <stdio.h>

template <class T>
class List {
    T* list;

public:
    int length;

    List(int len) {
        list = new T[len];
        length = len;
    }

    virtual ~List() {
        delete[] list;
    }

    T get(int index) {
        return list[index];
    }

    void set(int index, T val) {
        list[index] = val;
    }
};
/*
    You shouldn't change the code below, unless you want to _temporarily_ change the main function while testing.
    Change it back when you're done.
*/
typedef struct Point_ {
    int x;
    int y;
} Point;

int main(){
    List<int> integers(10);
    for(int i = 0; i < integers.length; i++){
        integers.set(i, i * 100);
        printf("%d ", integers.get(i));
    }
    printf("\n"); // this loop should print: 0 100 200 300 400 500 600 700 800 900

    List<Point *> points(5);
    for(int i = 0; i < points.length; i++) {
        Point *p = new Point;
        p->x = i * 10;
        p->y = i * 100;
        points.set(i, p);
        printf("(%d, %d) ", points.get(i)->x, points.get(i)->y);
        delete p;
    }
    printf("\n"); // this loop should print: (0, 0) (10, 100) (20, 200) (30, 300) (40, 400)
}

像这样用 g++ 编译:

g++ -Wall p2_templates.cpp -o p2_templates

通过此命令使用 valgrind:

valgrind --tool=memcheck ./p2_templates

从 valgrind 得到这个结果:

==22396== Memcheck, a memory error detector
==22396== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22396== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22396== Command: ./p2_templates
==22396== 
0 100 200 300 400 500 600 700 800 900 
(0, 0) (10, 100) (20, 200) (30, 300) (40, 400) 
==22396== 
==22396== HEAP SUMMARY:
==22396==     in use at exit: 72,704 bytes in 1 blocks
==22396==   total heap usage: 9 allocs, 8 frees, 73,848 bytes allocated
==22396== 
==22396== LEAK SUMMARY:
==22396==    definitely lost: 0 bytes in 0 blocks
==22396==    indirectly lost: 0 bytes in 0 blocks
==22396==      possibly lost: 0 bytes in 0 blocks
==22396==    still reachable: 72,704 bytes in 1 blocks
==22396==         suppressed: 0 bytes in 0 blocks
==22396== Rerun with --leak-check=full to see details of leaked memory
==22396== 
==22396== For counts of detected and suppressed errors, rerun with: -v
==22396== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

最佳答案

首先非常仔细和完整地阅读 valgrind 输出(并按照 valgrind 的建议进行所有操作)。在您的情况下,感兴趣的片段是:

Rerun with --leak-check=full to see details of leaked memory

still reachable: 72,704 bytes in 1 blocks

您还应该了解以下内容: http://valgrind.org/docs/manual/faq.html#faq.reports (4.1. 我的程序使用 C++ STL 和字符串类。Valgrind 报告在程序退出时涉及这些类的“仍可访问”内存泄漏,但应该没有)。

如果您有时间,您可能会从对此的详细了解中受益匪浅:http://valgrind.org/docs/manual/mc-manual.html#mc-manual.leaks (4.2.8. 内存泄漏检测)。

最后,您可以在这里找到大量有用的信息:http://valgrind.org (valgrind 主页)。

关于c++ - 指针和整数模板的内存泄漏。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855315/

相关文章:

c++ - gluPerspective 在 OpenGL 3.1 中被删除,有什么替代品吗?

c++ - 如何通过使其中一个必须是显式的来解决对模糊构造函数的调用?

javascript - 无法正确使用 Handlebars.js 在我的 'template' 中显示变量

objective-c - 这里有内存泄漏吗?

c++ - 我怎样才能产生一个只在给定时间内存在的进程

c++ - 我正在使用 Qt GUI c++ 开发 POS 系统项目

c++ - 模板函数的两个特化(int 和 int*)。编译错误

c++ - 在 std::unordered_map 中使用模板化键

iphone - appendFormat 泄漏

tomcat - Tomcat 6 是否已准备好进行持续集成或如何让它工作?