00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // 00015 00016 #include "CommonFramework.h" 00017 00018 // 00019 // Definition of the CTestOne class 00020 // 00021 class CTestOne : public CBase 00022 { 00023 public: 00024 ~CTestOne(); 00025 void SetTextL(const TDesC& aData); 00026 private : 00027 HBufC* iText; 00028 }; 00029 00030 00031 // 00032 // Implementation of the CTestOne class 00033 // 00034 _LIT(KTxtInsideDestructor,"Executing the CTestOne destructor\n"); 00035 CTestOne::~CTestOne() 00036 { 00037 delete iText; 00038 console->Printf(KTxtInsideDestructor); 00039 } 00040 00041 void CTestOne::SetTextL(const TDesC& aData) 00042 { 00043 if (iText) 00044 { 00045 delete iText; 00046 iText = NULL; 00047 } 00048 iText = aData.AllocL(); 00049 } 00050 00051 00052 00053 // 00054 // Definition of the RTestTwo class 00055 // 00056 class RTestTwo 00057 { 00058 public: 00059 RTestTwo(TInt aValue); 00060 void Close(); 00061 private : 00062 TInt iX; 00063 }; 00064 00065 // 00066 // Implementation of the RTestTwo class 00067 // 00068 RTestTwo::RTestTwo(TInt aValue) 00069 : iX(aValue) 00070 { 00071 } 00072 00073 00074 _LIT(KTxtCloseRTestTwo,"RTestTwo closing\n"); 00075 00076 void RTestTwo::Close() 00077 { 00078 console->Printf(KTxtCloseRTestTwo); 00079 } 00080 00081 00082 00083 // 00084 // Definition of the RTestThree class 00085 // 00086 class RTestThree 00087 { 00088 public: 00089 RTestThree(TInt aValue); 00090 void Release(); 00091 private : 00092 TInt iY; 00093 }; 00094 00095 // 00096 // Implementation of the RTestThree class 00097 // 00098 RTestThree::RTestThree(TInt aValue) 00099 : iY(aValue) 00100 { 00101 } 00102 00103 00104 _LIT(KTxtReleaseRTestThree,"RTestThree releasing\n"); 00105 00106 void RTestThree::Release() 00107 { 00108 console->Printf(KTxtReleaseRTestThree); 00109 } 00110 00111 // 00112 // main body of the example 00113 // 00114 _LIT(KTxtHelloWorld,"Hello World!"); 00115 LOCAL_C void doExampleL() 00116 { 00117 // 1. Construct a CTestOne object on the heap 00118 CTestOne* one = new (ELeave) CTestOne; 00119 00120 // Use the CleanupDeletePushL() function to put a TCleanUpItem 00121 // on the cleanup stack 00122 CleanupDeletePushL(one); 00123 00124 // Exercise the CTestOne object (just to show it doing something) 00125 one->SetTextL(KTxtHelloWorld); 00126 00127 // Pop and destroy the cleanup item off the cleanup stack. 00128 // The cleanup operation deletes the CTestOne object 00129 CleanupStack::PopAndDestroy(); 00130 00131 // 2. Construct a RTestTwo object on the program stack. 00132 // 00133 // The value passed is of no significance; it is just 00134 // to show that the class is not trivial. 00135 RTestTwo two(2); 00136 00137 // Use the CleanupClosePushL() function to put a TCleanUpItem 00138 // on the cleanup stack 00139 CleanupClosePushL(two); 00140 00141 // Pop and destroy the cleanup item off the cleanup stack. 00142 // The cleanup operation calls the Close() member function of 00143 // the RTestTwo object 00144 CleanupStack::PopAndDestroy(); 00145 00146 00147 // 3. Construct a RTestThree object on the program stack. 00148 // 00149 // The value passed is of no significance; it is just 00150 // to show that the class is not trivial. 00151 RTestThree three(3); 00152 00153 // Use the CleanupClosePushL() function to put a TCleanUpItem 00154 // on the cleanup stack 00155 CleanupReleasePushL(three); 00156 00157 // Pop and destroy the cleanup item off the cleanup stack. 00158 // The cleanup operation calls the Release() member function of 00159 // the RTestThree object 00160 CleanupStack::PopAndDestroy(); 00161 00162 // 4. Construct an array of objects on the heap 00163 TInt heapSize1 = User::Heap().Count(); // take a count of the heap size now 00164 00165 const TInt KNumObjects = 4; 00166 const TInt KStringLength = 10; 00167 TBuf<KStringLength>* four = new (ELeave) TBuf<KStringLength>[KNumObjects]; 00168 // Use the CleanupArrayDeletePushL() function to put a TCleanUpItem 00169 // on the cleanup stack 00170 CleanupArrayDeletePushL(four); 00171 00172 // Do something that might leave - a simple memory allocation 00173 TInt* mem = (TInt*)User::Alloc(100); 00174 delete mem; 00175 00176 // Pop and destroy the cleanup item off the cleanup stack. 00177 // The cleanup operation deletes the CTestOne object 00178 CleanupStack::PopAndDestroy(); 00179 if ( User::Heap().Count() == heapSize1 ) 00180 { 00181 _LIT(KFourComplete,"Array deleted\n"); 00182 console->Printf(KFourComplete); 00183 } 00184 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.