一种内存泄漏的代码
内存泄漏的地方经常比较隐秘,今天就遇到了一回。问题出在delete (void*)上面,示例代码如下:
class Detect { public: Detect() { m_n1 = 0; m_n2 = 0; m_data.resize(10 , 0); } virtual ~Detect() { } protected: int m_n1; int m_n2; deque<short> m_data; }; class Monitor { public: Monitor() { m_pDetect = new Detect; } ~Monitor() { if (m_pDetect != 0) {
// 加上这行代码就避免泄漏内存 //((Detect*)m_pDetect)->~Detect(); delete m_pDetect; m_pDetect = 0; } } bool detect() { return true; } protected: void* m_pDetect; };