在路上...

只学不做等于没学,只知不做等于无知!

Symbian 防止RPointerArray内存泄露

#include "CommonFramework.h"

class CTest : public CBase
    {
public:
    staticCTest* NewLC(const TDesC& aName)
       {
       CTest* self = new (ELeave) CTest();
       CleanupStack::PushL(self);
       self->ConstructL(aName);
       return self;
       }
    staticCTest* NewL(const TDesC& aName)
       {
       CTest* self = NewLC(aName);
       CleanupStack::Pop(self);
       return self;
       }
    voidDisplay()
       {
       console->Printf(_L("%S/n"), iName);
       }
   ~CTest()
       {
       console->Printf(_L("~CTest() called./n"));
       delete iName;
       }
protected:
   CTest()
       {
       }
    voidConstructL(const TDesC& aName)
       {
       iName = aName.AllocL();
       }
private:
    HBufC*iName;
    };

template <typename T>
void ReleaseRPointerArray(TAny* aArray)
    {
   RPointerArray<T>* array =static_cast<RPointerArray<T>*>(aArray);
   array->ResetAndDestroy();
   array->Close();
    }

LOCAL_C void doExampleL()
    {
   RPointerArray<CTest>* array = new(ELeave) RPointerArray<CTest>;
   CleanupStack::PushL(TCleanupItem(ReleaseRPointerArray<CTest>,array));

   _LIT(KName1, "Allan");
    _LIT(KName2,"George");
    _LIT(KName3,"Larry");

    CTest*test1 = CTest::NewL(KName1);
   CleanupStack::PushL(test1);
   array->AppendL(test1);
   CleanupStack::Pop(test1);

    CTest*test2 = CTest::NewL(KName2);
   CleanupStack::PushL(test2);
   array->AppendL(test2);
   CleanupStack::Pop(test2);

    CTest*test3 = CTest::NewL(KName3);
   CleanupStack::PushL(test3);
   array->AppendL(test3);
   CleanupStack::Pop(test3);

   RPointerArray<CTest>&pointerArray = *array;
    for (TIntindex = array->Count(); --index >=0;)
       {
       pointerArray[index]->Display();
       }
  
   CleanupStack::PopAndDestroy(array);
    }

 

阅读更多
个人分类: Symbian
想对作者说点什么? 我来说一句

symbian 内存泄露检测工具 hookloger

2011年03月21日 1.66MB 下载

JVM内存泄露

2011年10月28日 144KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭