Symbian OS S60 3rd platform 开发入门
2010-03-24 03:56:00 来源:WEB开发网Developers who write a C-class and then forget to derive it from CBase will leave themselves vulnerable to memory leaks. This is because objects pushed onto the Cleanup Stack that are not derived from CBase are pushed as TAny*. No destructor will be called when these objects are explicitly removed from the stack, either via PopAndDestroy() or when the stack implicitly performs cleanup upon leaving. (Note that it is fine for pointers to some objects, such as T-class objects, to be pushed onto the Cleanup Stack as TAny*, because they do not have destructors.) For this reason, remember to derive all C-classes from CBase.
Dangling pointers are easily created by forgetting to set pointers to NULL when explicitly deleting their corresponding objects. It is important to do this even if the next line of code allocates a new value, because the allocation itself may leave. One exception to this rule is in destructors, as member data pointers will fall out of scope as their class instance is destroyed.
Constructors and destructors should not call leaving functions. Doing this compromises the failsafe memory management measures offered by two-phase construction. Therefore, never call in a C++ constructor or destructor a function that can leave.
Virtual functions should not appear in constructors and destructors. The intention of a virtual function is to provide transparent access to its most derived implementation. This polymorphism feature is not available in constructors and destructors because the derived class will not exist at this time. In other words, the constructor of base class A does not know that it is actually creating a derived class B, and therefore a "virtual" function call within the constructor will only call class A's implementation.
Explicitly deleting an item that has already been placed on the Cleanup Stack is a mistake. By placing an item on the Cleanup Stack, you are
更多精彩
赞助商链接