examples/Base/MemMan/Cleanup/ELeaveOnFail/ELeaveOnFail.cpp

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 // Example shows attempt to construct an object and specifying
00015 // (ELeave) after the 'new' operator. 
00016 // Specifying (ELeave) causes a leave on failure to allocate memory 
00017 // for the new object. 
00018 // NOTE: the structure of this example is different to standard E32 examples
00019 //
00020 
00021 
00022 #include <e32cons.h>
00023 
00024 //
00025 // Common format text
00026 //
00027 
00028 _LIT(KCommonFormat1,"Value of iInt is %d.\n");
00029 
00030 
00031   // All messages written to this
00032 LOCAL_D CConsoleBase* console;
00033   
00034   // Flag which determines whether the doSomething() member function
00035   // of the CExample class should leave when called.
00036 LOCAL_D TBool leaveFlag = ETrue;
00037 
00038   // Parameter for __UHEAP_SETFAIL
00039   // Allocation guaranteed to fail at this number of allocation attempts;
00040   // i.e. if set to n, allocation fails on the nth attempt. 
00041   // NB only used in debug mode
00042 #ifdef _DEBUG
00043 LOCAL_D TInt allocFailNumber = 1;
00044 #endif
00045   
00046   // Function prototypes
00047 LOCAL_C void doExampleL();      
00048 LOCAL_C void callExampleL();
00049 
00050 
00051 
00053 //
00054 // -----> CExample (definition)
00055 //
00056 // The class is used by the example code
00057 //
00059 class CExample : public CBase
00060         {
00061 public :
00062         void DoSomethingL();
00063 public :
00064         TInt iInt;
00065         };
00066 
00067 
00069 //
00070 // -----> CExample (implementation)
00071 //
00073 void CExample::DoSomethingL() 
00074         {
00075           // Leave if the global flag is set
00076         if (leaveFlag)
00077                 {
00078                 _LIT(KMsgLeaving,"DoSomethingL leaving.\n");
00079                 console->Printf(KMsgLeaving);
00080                 User::Leave(KErrGeneral);               
00081                 }
00082         console->Printf(KCommonFormat1,iInt);
00083         }
00084 
00086 //
00087 // Main function called by E32
00088 //
00090 GLDEF_C TInt E32Main()
00091     {
00092           // Get cleanup stack
00093         CTrapCleanup* cleanup=CTrapCleanup::New();
00094 
00095           // Some more initialization, then do the example
00096         TRAPD(error,callExampleL());
00097           
00098           // callExampleL() should never leave.
00099         _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
00100         __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
00101 
00102           // destroy the cleanup stack
00103         delete cleanup;
00104         
00105           // return
00106         return 0;
00107     }
00108 
00109 
00111 //
00112 //
00113 //
00115 LOCAL_C void callExampleL() 
00116     {
00117           // Initialize and call the example code under cleanup stack.
00118         _LIT(KMsgExampleCode,"Symbian platform Example Code");
00119         console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00120           // Put console onto the cleanup stack.
00121         CleanupStack::PushL(console);
00122 
00123           // Perform the example function under the protection of a 
00124           // TRAP harness.
00125         TRAPD(error,doExampleL());
00126         _LIT(KMsgOK,"ok");
00127         _LIT(KFormat2,"failed: leave code = %d");
00128         if (error)
00129                 console->Printf(KFormat2,error);
00130         else
00131                 console->Printf(KMsgOK);
00132           
00133           // Continue
00134         _LIT(KMsgPressAnyKey," [press any key]");
00135         console->Printf(KMsgPressAnyKey);
00136         console->Getch();
00137 
00138           // Remove the console object from the cleanupstack
00139           // and destroy it. 
00140         CleanupStack::PopAndDestroy();
00141     }
00142 
00143 
00145 //
00146 // Do the example
00147 //
00149 void doExampleL()
00150         {
00151           // Memory alloc fails on the 'allocFailNumber' attempt. 
00152           //
00153           // Note that if you set this to some low value sucah as 1 or 2, then as a side effect,
00154           // you may also see a dialog box stating "Not enough memory".
00155         __UHEAP_SETFAIL(RHeap::EDeterministic,allocFailNumber);
00156           // Allocate - leave if allocation fails.
00157           // The (ELeave) causes a leave if allocation fails; replaces
00158           // a call to User::LeaveIfNull(myExample);
00159           // Compare with the code in LeaveOnFailure
00160           //
00161           // Note that the memory allocation will fail and the "new" operation
00162           // will leave if allocFailNumber is 1.
00163         CExample* myExample = new (ELeave) CExample;
00164           // Do something with the CExample object
00165         myExample->iInt = 5;
00166           //
00167         console->Printf(KCommonFormat1,myExample->iInt);
00168           // Delete the CExample object
00169         delete myExample;
00170         }
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 

Generated by  doxygen 1.6.2