异构数据收集器 - to make logic b…

本文介绍了一种自动实施内存释放的类/API,通过两种方法实现,避免了直接使用void*存储各种指针导致的对象实例析构函数不被调用的问题。通过示例展示了如何简化复杂逻辑并确保资源正确回收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//====================================================================================
//  A class/API to automatically implement memory de-allocation.
//        two method can be used. refer to test case test(), test1()
//  Specially, you cannot directly use void * to store kinds of pointer,otherwise
//  de-structor of object instances will not be called.
//
//                X.G. ZHANG
, 2009-01-10
//====================================================================================
//  
If there is complex logic, such API can keep logic in simple style, for example
//          allocate memory, and CMemCleander <TYPE> deletor(pointer, if_array)
//          if(a)
//          {
//              return;
   // no need deallocate, object destructor automatically do it
//          }


#define AUTO_DELETE(deletor,TYPE, pointer, if_array)  CMemCleanderEx <TYPE> \
                                                      deletor(pointer,if_array)



// base class

class IMemCleanerEx
{
public:
     virtual void Reset() = 0;
public:
    virtual ~IMemCleanerEx(){}
};

// sub-class using template to collect different kinds of pointer, array or non-array
template <class TYPE>
class CMemCleanerEx:public IMemCleanerEx
{
public:
    CMemCleanerEx(TYPE*& pData, BOOL isArray):m_pData(pData),m_IsArray(isArray)
    {
    }
    virtual void Reset()
    {
        if(m_pData)
        {
            if(m_IsArray)
            {
                delete []m_pData;
            }
            else
            {
                delete m_pData;
            }
        }

        m_pData = NULL;
    }
public:
    virtual ~CMemCleanerEx()
    {
        Reset();
    }
protected:
    CMemCleanerEx()
    {
        printf("Disabled!");
    }
protected:
    TYPE*& m_pData;
    BOOL   m_IsArray;
};

// 异构数据收集器, 自动垃圾收集
class CMemAutoDeletor
{
    struct MemNode
    {
       IMemCleanerEx* m_Data;
       MemNode* m_pNext;
    };

public:
    void AddCleaner(IMemCleanerEx* pMC)
    {
        if(m_pHeader)
        {
            CMemAutoDeletor::MemNode* pNode = NULL;
            for(pNode = m_pHeader;pNode->m_pNext!=NULL; pNode = pNode->m_pNext)
            {
                // do nothing la
            }

            CMemAutoDeletor::MemNode* pNode1 = new MemNode();

            pNode1->m_Data = pMC;
            pNode1->m_pNext = NULL;

            pNode->m_pNext = pNode1;
        }
        else
        {
            CMemAutoDeletor::MemNode* pNode = new MemNode();
            pNode->m_Data = pMC;
            pNode->m_pNext = NULL;

            m_pHeader = pNode;
        }
    
    }
   
public:
    void Reset()
    {
        CMemAutoDeletor::MemNode* pNode = NULL;

        for(;m_pHeader;m_pHeader = pNode)
        {
            pNode = m_pHeader->m_pNext;

            delete m_pHeader->m_Data;
            delete m_pHeader;               
        }

        m_pHeader = NULL;   
    }
public:
    CMemAutoDeletor()
    {
        m_pHeader = NULL;   
    }
    virtual ~CMemAutoDeletor()
    {
        Reset();   
    }
protected:
    CMemAutoDeletor::MemNode* m_pHeader;
};

// 测试代码
class CC  // test class, just to check whether destructor is called
{
public:
    ~CC()
    {
        printf("CC:~CC() is called, destructor is called, :-P\r\n");
        TRACE("CC:~CC() is called, destructor is called, :-P\r\n");
    }
};

// test case 1, using CMemAutoDeletor
void Test()
{
  
    CMemAutoDeletor deletor;

    int* iArray = new int[1000];
    deletor.AddCleaner(new CMemCleanerEx<int>(iArray, TRUE));  // this is an int array

    CC* pCCArray = new CC[100];
    deletor.AddCleaner(new CMemCleanerEx<CC>(pCCArray,TRUE)); // this is a CC array

    CC* pCC = new CC();
    deletor.AddCleaner(new CMemCleanerEx<CC>(pCC,FALSE));     // a simple CC instance

    int* pI = new int;
    *pI = 100;
    AUTO_DELETE(deletor4,int,pI,FALSE);    // and int variable, not an array, use macro, :-P

    // before quit, destructor of deletor will automatically implement garbage collection

}

// 测试例程二,直接使用CMemCleaner
void test2()
{
    // in this test case, deletor1, deletor2, deletor3, deletor4 will automatically
    // implement memory free.
    int* iArray = new int[1000];
    CMemCleanerEx<int> deletor1(iArray, TRUE);  // this is an int array   

    CC* pCCArray = new CC[100];
    CMemCleanerEx<CC>  deletor2(pCCArray,TRUE); // this is a CC array

    CC* pCC = new CC();
    CMemCleanerEx<CC> deletor3(pCC,FALSE);     // a simple CC instance

    int* pI = new int;
    *pI = 100;
    CMemCleanerEx<int> deletor4(pI,FALSE);    // and int variable, not an array

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值