情况源于我的之前一片博客《C++ 智能指针》,在我写demo代码的时候。
向前申明了class Phone, 然后再U_ptr类析构函数中delete Phone的指针。
出现warning C4150: 删除指向不完整“XXX”类型的指针;没有调用析构函数
这个waring会导致内存泄露。前向申明的类的析构函数没有被调用
出现warning的代码如下:
#include <iostream> using namespace std; class ConnectManager; class CommManager; class Phone; class U_ptr { friend class ConnectManager; friend class CommManager; public: U_ptr(Phone *pH): use(0), pPhone(pH) { } ~U_ptr() { delete pPhone; } private: Phone *pPhone; size_t use; }; class Phone { public: Phone() { cout << "Hello World!" << endl; } ~Phone() { cout << "Bye!" << endl; } }; class ConnectManager { public: ConnectManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "ConnectManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~ConnectManager() { cout << "ConnectManaer: Can I delete the Phone?" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; delete pUptr; } else { cout << "No, You can't, The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; class CommManager { public: CommManager(U_ptr *p): pUptr(p) { ++pUptr->use; cout << "CommManager: Hi I get the Phone" << endl; cout << pUptr->use << " users" << endl; } ~CommManager() { cout << "CommManager: Can I delete the Phone" << endl; if (--pUptr->use == 0) { cout << "Yes, You can" << endl; } else { cout << "No, You can't. The Phone is in use" << endl; cout << pUptr->use << " users" << endl; } } private: U_ptr *pUptr; }; int main(void) { Phone *symbian = new Phone(); U_ptr *pU = new U_ptr(symbian); ConnectManager connManager = ConnectManager(pU); CommManager commManager = CommManager(pU); }
出现原因:
class Phone;这种方式向前申明,其后面的类只能申明其指针,前向申明以后的类无法看到其类实体。
所以,delete的时候,Phone的析构函数对后面的类是透明不可见的,除非使用头文件包含。