//----------------------------------------------------------------------------- // Pointer_CX.h (checking implementation) //----------------------------------------------------------------------------- #ifndef POINTER_CX_H #define POINTER_CX_H #include "__Pointer_MapX.h" extern __Pointer_MapX __pointer_mapX; #define New(p) p.Allocate (__LINE__, __FILE__) #define Delete(p) p.Deallocate (__LINE__, __FILE__) template class Pointer_CX { public: Pointer_CX () { timestamp = -1; } ~Pointer_CX () { if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Decrement_Ref_Count (rep); if (__pointer_mapX.Ref_Count (rep) == 0) { __pointer_mapX.Report_Error ( "Creating memory leak by pointer leaving scope", 0, "" ); } } } Pointer_CX (const Pointer_CX& p) { rep = p.rep; timestamp = p.timestamp; if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Increment_Ref_Count (rep); } } Pointer_CX (void* a) { rep = (T*) a; } Pointer_CX& operator= (const Pointer_CX& p) { if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Decrement_Ref_Count (rep); if (__pointer_mapX.Ref_Count (rep) == 0) { __pointer_mapX.Report_Error ( "Creating memory leak by using = (i.e., assignment)", 0, "" ); } } rep = p.rep; timestamp = p.timestamp; if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Increment_Ref_Count (rep); } return *this; } Pointer_CX& operator= (const void* a) { if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Decrement_Ref_Count (rep); if (__pointer_mapX.Ref_Count (rep) == 0) { __pointer_mapX.Report_Error ( "Creating memory leak by using = NULL (i.e., assignment)", 0, "" ); } } // rep = static_cast (a); rep = (T*) a; return *this; } bool operator== (const Pointer_CX& p) { if (((rep != 0) && (! __pointer_mapX.Contains (rep, timestamp))) || ((p.rep != 0) && (!__pointer_mapX.Contains (p.rep, p.timestamp)))) { __pointer_mapX.Report_Error ( "Using dead pointer with == (i.e., equality checking)", 0, "" ); } return rep == p.rep; } bool operator== (const void* a) { if ((rep != 0) && (! __pointer_mapX.Contains (rep, timestamp))) { __pointer_mapX.Report_Error ( "Using dead pointer with == (i.e., equality checking)", 0, "" ); } return rep == (T*) a; } bool operator!= (const Pointer_CX& p) { if (((rep != 0) && (! __pointer_mapX.Contains (rep, timestamp))) || ((p.rep != 0) && (!__pointer_mapX.Contains (p.rep, p.timestamp)))) { __pointer_mapX.Report_Error ( "Using dead pointer with != (i.e., inequality checking)", 0, "" ); } return rep != p.rep; } bool operator!= (const void* a) { if ((rep != 0) && (! __pointer_mapX.Contains (rep, timestamp))) { __pointer_mapX.Report_Error ( "Using dead pointer with != (i.e., inequality checking)", 0, "" ); } return rep != (T*) a; } void Allocate (long l, char* f) { if (__pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Decrement_Ref_Count (rep); if (__pointer_mapX.Ref_Count (rep) == 0) { __pointer_mapX.Report_Error ( "Creating memory leak by using New", l, f ); } } rep = new T; __pointer_mapX.Add (rep, timestamp, l, f); __pointer_mapX.Increment_Ref_Count (rep); } void Deallocate (long l, char* f) { if (rep != 0) { if (! __pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Report_Error ( "Deleting dead pointer", l, f ); } __pointer_mapX.Remove (rep); } delete rep; } T* operator-> () { if (rep == 0) { __pointer_mapX.Report_Error ( "Dereferencing null pointer by using ->", 0, "" ); } if (! __pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Report_Error ( "Dereferencing dead pointer by using ->", 0, "" ); } return rep; } T& operator* () { if (rep == 0) { __pointer_mapX.Report_Error ( "Dereferencing null pointer by using *", 0, "" ); } if (! __pointer_mapX.Contains (rep, timestamp)) { __pointer_mapX.Report_Error ( "Dereferencing dead pointer by using *", 0, "" ); } return *rep; } private: T* rep; long timestamp; }; extern void Report_Storage_Allocation (); #endif // POINTER_CX_H