这段c++代码有内存泄露?

 红昊子楽楽七_358 发布于 2022-11-02 08:00

学习c++过程中, 自己试着实现了一个简单的栈, 结果用valgrind检测发现可能发生内存泄露, 但是一直找不到原因...
代码如下 :

#ifndef RE_STACK_H
#define RE_STACK_H

#include 
#include "general.h"

template 
class Stack {
private:
    T* bottom;
    T* top;
    T* sentinel;
    uint32 size;
public:
    Stack(uint32 size = 30);
    ~Stack();
    void push(const T&);
    T pop();
    void expand();
};

template 
Stack::Stack(uint32 size) :size(size){
    bottom = top = static_cast(malloc(size * sizeof(T)));
    sentinel = bottom + size * sizeof(T);
}

template 
Stack::~Stack() {
    while (top-- != bottom){
        top->~T();
    }
    free(bottom);
}

template 
void Stack::push(const T& item){
    if(top == sentinel){
        expand();
    }
    new(top++)T(item);
}

template 
T Stack::pop() {
    if(bottom == top){
        return 0;
    }
    T re = *--top;
    top->~T();
    return re;
}

template 
void Stack::expand() {
    size *= 2;
    uint32 temp = top - bottom;
    bottom = static_cast(realloc(bottom, size * sizeof(T)));
    top = bottom + temp;
    sentinel = bottom + size * sizeof(T);
}
#endif //RE_STACK_H

valgrind错误信息如下 :

==32859== HEAP SUMMARY:
==32859==     in use at exit: 22,216 bytes in 190 blocks
==32859==   total heap usage: 256 allocs, 66 frees, 27,992 bytes allocated
==32859== 
==32859== 2,064 bytes in 1 blocks are possibly lost in loss record 58 of 63
==32859==    at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==32859==    by 0x1005E1EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D5182: protocols() (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D5093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D2C13: gc_init (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005DA24E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005E7132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D583C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D5300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==32859==    by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==32859== 
==32859== LEAK SUMMARY:
==32859==    definitely lost: 0 bytes in 0 blocks
==32859==    indirectly lost: 0 bytes in 0 blocks
==32859==      possibly lost: 2,064 bytes in 1 blocks
==32859==    still reachable: 0 bytes in 0 blocks
==32859==         suppressed: 20,152 bytes in 189 blocks
==32859== 
==32859== For counts of detected and suppressed errors, rerun with: -v
==32859== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 18)

我的main函数里面只有一句话, 就是return 0; 但是头文件中包括了上面这个文件...

#ifndef RE_GENERAL_H
#define RE_GENERAL_H

#ifndef NULL
#define NULL 0
#endif

using uint8 = unsigned char;
using uint32 = unsigned int;
using int32 = int;

#endif //RE_GENERAL_H
#include 
#include "NTL/Stack.h"

int main() {
    
    return 0;
}
2 个回答
  • valgrind 能用来检测c++ 的程序吗

    2022-11-04 04:25 回答
  • 讲道理我是不相信的。。。最好贴出你的测试代码,以及general.h的内容。

    2022-11-04 04:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有