操作员new(unsigned long)的内存泄漏?

时间:2015-10-07 14:57:40

标签: valgrind

这是什么意思?

  • 我在那个班里没有动态分配?

这是cunstructurs类apprantley选择泄漏

 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 log outpot: 我正在使用ubunto 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)

1 个答案:

答案 0 :(得分:1)

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

  

每个new()必须与delete()

匹配

我假设(但看不到)create::DoCommand(std::string*, OS&)new()课程内memory,而没有相应的delete()