valgrind报告operator new(unsigned long)的内存泄漏?

3

这是什么意思?

  • 在那个类中我没有任何动态分配内存?

这是构造函数类似地导致了泄漏。

 class memory :public Moudle{
        private:
            map<string, Moudle*> oData;
            string id;
            int size;
            int currsize;
            bool alive;
        public:
            memory();
            memory(int _i,string _id):Moudle(Memory){ 
            id = _id; 
            size = _i;
            alive = true; 
            currsize = 0;
            }
            bool isalive(){ return alive; }
            void kill(){ alive = false; }
            string getid(){ return id; }
            virtual void stop();
            void del(string id){ oData.erase(id); }
            bool find(string id);
            Moudle* getMoudle(string id);
            void insertFile(string id, Moudle* file);
            virtual string read() { return ""; }
            virtual void addElem(int index, string val) {}
            bool isfull(){ return (currsize>size || currsize == size); }
            string print(int mode);
        };

这是valgrind的日志输出: 我使用的是Ubuntu Linux,并在g++ 98中编译。
==25429== Memcheck, a memory error detector
==25429== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25429== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==25429== Command: ./prog
==25429== Parent PID: 5070
==25429== 
==25429== 
==25429== HEAP SUMMARY:
==25429==     in use at exit: 546 bytes in 12 blocks
==25429==   total heap usage: 85 allocs, 73 frees, 13,062 bytes allocated
==25429== 
==25429== 546 (88 direct, 458 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==25429==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25429==    by 0x402D13: create::DoCommand(std::string*, OS&) (in /home/naor_pc/Desktop/1/prog)
==25429==    by 0x40ADC1: operatCreate(std::string) (in /home/naor_pc/Desktop/1/prog)
==25429==    by 0x40B856: OS::runShell() (in /home/naor_pc/Desktop/1/prog)
==25429==    by 0x406CA8: main (in /home/naor_pc/Desktop/1/prog)
==25429== 
==25429== LEAK SUMMARY:
==25429==    definitely lost: 88 bytes in 1 blocks
==25429==    indirectly lost: 458 bytes in 11 blocks
==25429==      possibly lost: 0 bytes in 0 blocks
==25429==    still reachable: 0 bytes in 0 blocks
==25429==         suppressed: 0 bytes in 0 blocks
==25429== 
==25429== For counts of detected and suppressed errors, rerun with: -v
==25429== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

最简单的例子(提交给 bugs.kde.org:https://bugs.kde.org/show_bug.cgi?id=459047):


void divide2DDoubleArray(double * &block, double ** &subblockdividers, int noofsubblocks, int subblocksize){
    /* The starting address of a block of doubles is used to generate
     * pointers to subblocks.
     *
     *      block: memory containing the original block of data
     *      subblockdividers: array of subblock addresses
     *      noofsubblocks: specify the number of subblocks produced
     *      subblocksize: specify the size of the subblocks produced
     *
     * Design by contract: application should make sure the memory
     * in block is allocated and initialized properly.
     */
    // Build 2D matrix for cols
    subblockdividers=new double *[noofsubblocks];
    subblockdividers[0]= block;
    for (int i=1; i<noofsubblocks; ++i) {
        subblockdividers[i] = &subblockdividers[i-1][subblocksize];
    }
}

int main(){
    
    double * testarray = new double[16];
    double ** pTestarray; 
    
    divide2DDoubleArray(testarray, pTestarray, 4, 4);
    
    delete[] pTestarray;
}

编译时使用的:

g++ -g valgrind_error.cpp -o ve.out

并运行

valgrind --leak-check=yes  ./ve.out 

产生:

==5775== Memcheck, a memory error detector
==5775== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5775== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5775== Command: ./ve.out
==5775== 
==5775== 
==5775== HEAP SUMMARY:
==5775==     in use at exit: 128 bytes in 1 blocks
==5775==   total heap usage: 3 allocs, 2 frees, 72,864 bytes allocated
==5775== 
==5775== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5775==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5775==    by 0x109283: main (valgrind_error.cpp:25)
==5775== 
==5775== LEAK SUMMARY:
==5775==    definitely lost: 128 bytes in 1 blocks
==5775==    indirectly lost: 0 bytes in 0 blocks
==5775==      possibly lost: 0 bytes in 0 blocks
==5775==    still reachable: 0 bytes in 0 blocks
==5775==         suppressed: 0 bytes in 0 blocks
==5775== 
==5775== For lists of detected and suppressed errors, rerun with: -s
==5775== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

涉及的代码仅使用指向双精度浮点数和指向双重指针的指针。然而,我们发现与“operator new[](在/ usr / lib / x86_64-linux-gnu / valgrind / vgpreload_memcheck-amd64-linux.so中)相关的内存泄漏”。这不是来自最小示例。


你有没有在不使用这个特定类的情况下测试过valgrind?此外,你从未将oData初始化为有意义的内容,尽管这并不是重点。 - Andrew Fan
每次我在代码中使用new时,都会出现这个错误。 - Naor Tedgi
1个回答

2

这里可能没有提供足够的代码示例,但原则应该是:

每个 new() 必须与一个 delete() 相匹配

我假设(但无法确定)在 create::DoCommand(std::string*, OS&) 中,您调用了一个 memory 类的 new(),但没有相应的 delete()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接