檢測內存泄漏以下是新/ delete操作符我在程序的模式。 Valgrind說,記憶「絕對失去」。我無法完全知道泄漏的位置。我使用新的/刪除操作符有什麼問題嗎?無法通過指甲的valgrind
class Generic
{
GenericInterface *gInterface; //GenericInterface is abstract class
public:
Generic()
{
gInterface = NULL;
}
~Generic()
{
delete gInterface;
}
void Create()
{
gInterface = new Specific();
}
};
class Specific : public GenericInterface
{
MyClass* _myClass;
public:
Specific()
{
_myClass = new MyClass;
}
~Specific()
{
delete _myClass;
}
};
int main()
{
Generic g;
g.Create();
}
valgrind說內存丟失。
==2639== 8 bytes in 1 blocks are definitely lost in loss record 2 of 45
==2639== at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==2639== by 0x804D77C: Specific::Specific() (Specific.cc:13)
==2639== by 0x804DAFC: Generic::Create() (Generic.cc:58)
當我把`虛擬〜GenericInterface()= 0;`,我得到未定義參考`〜GenericInterface()`錯誤`具體`析構函數。 – Meebo 2011-01-29 00:01:22