2) During deallocation I find address array and if the address exists I delete allocated memory address and erase the integer address from address array.
if(_ptr != NULL){// Find if adress exists in array m_it = find(m_tAddressArray.begin(), m_tAddressArray.end(), _ptr );if(m_it != m_tAddressArray.end()) {// If adress exists delete pointerdelete [] _ptr; _ptr = NULL;// Delete adress from array m_tAddressArray.erase(m_it); }}
3) Test:
#include "K3dSafePointer.h"usingnamespace K3d;int main(int argc, char *argv[]){// Create safe pointer for integer and float K3dSafePointer<int> kSpInt; K3dSafePointer<float> kSpFloat;// Test pointersint *pInt = NULL;int *pInt2 = NULL;int *pInt3 = NULL;float *pFloat = NULL;float *pFloat2 = NULL;// Safe allocation equivalent to "pInt = new int" pInt = kSpInt.SafeNew();// Safe allocation of int array equivalent to "pInt3 = new int[5]" pInt3 = kSpInt.SafeNew(5);// Safe allocation equivalent to "pFloat = new float") pFloat = kSpFloat.SafeNew();// Safe allocation equivalent to "pFloat2 = new float[10]" pFloat2 = kSpFloat.SafeNew(10);// Test copy pInt to pInt2 *pInt = 1; pInt2 = pInt; *pInt = 3; *pInt2 = 4;// Test int array pInt3[0] = 0; pInt3[2] = 2; pInt3[3] = 3;int iTest = pInt3[0]; iTest = pInt3[2]; iTest = pInt3[3];// Test pointer to float *pFloat = (float) 2.22; *pFloat2 = (float) 3.33;// Safe delete equivalent to "delete pInt" pInt = kSpInt.SafeDelete(pInt);// Safe delete equivalent to "delete pInt2". // The pInt2 is the same address like pInt, because I copy pInt to pInt2 (pInt2 = pInt;)// Without using safe pointer would be called error (SIGABR double free or corruption) pInt2 = kSpInt.SafeDelete(pInt2);// Safe delete equivalent to "delete pInt3[]". pInt3 = kSpInt.SafeDelete(pInt3);// Safe delete equivalent to "delete pFloat" pFloat = kSpFloat.SafeDelete(pFloat);// Safe delete equivalent to "delete pFloat[]" pFloat2 = kSpFloat.SafeDelete(pFloat2);// Test delete already deleted pointer// Without using safe pointer would be called error (SIGABR double free or corruption) pFloat2 = kSpFloat.SafeDelete(pFloat2);return0;}