In C ++, you do not have a built-in cross-platform function that will detect if the pointer is deleted.
However, you can use the tools provided by some debuggers, tools, and the language itself. For example, you can overload the new
and delete
operator globally and / or based on each class and support a common link type / map type. eg:.
class X { ... set<void*> m_CurrentAlloc; public: void* operator new (size_t SIZE) { ... m_CurrentAlloc.insert(p); return p; } void operator delete (void *p) { m_CurrentAlloc.erase(p); ... } };
At periodic bases or at the end of a program, the contents of this set
can be printed or verified.
Remember that this is the solution for the ideal situation when you are doing memory management with new/delete
. If you have a combination of malloc/free
, then the code also needs other improvements.
source share