examples/Base/MemMan/Cleanup/SimpleOOM/SimpleOOM.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 checks the robustness of a simple class on Out Of Memory (OOM)
00015 // NOTE: the structure of this example is different to standard E32 examples
00016 //
00017 
00018 
00019 #include <e32cons.h>
00020 
00021 
00022 
00023   // All messages written to this
00024 LOCAL_D CConsoleBase* console;
00025   
00026   // Function prototypes
00027 LOCAL_C void doExampleL();      
00028 LOCAL_C void callExampleL();
00029 
00030 
00032 //
00033 // -----> CSimple (definition)
00034 //
00036 class CSimple : public CBase
00037         {
00038 public :
00039         static CSimple* NewL(TInt aVal);
00040         static CSimple* NewLC(TInt aVal);
00041         void   Display();
00042 protected: 
00043         CSimple(TInt aVal);
00044 public: 
00045         TInt   iVal;
00046         };
00047 
00048 
00050 //
00051 // -----> CSimple (implementation)
00052 //
00054 CSimple* CSimple::NewL(TInt aVal) 
00055         {
00056           // NB The NewL function uses the C++ constructor mechanism.
00057         CSimple* self=new (ELeave) CSimple(aVal);
00058         return self;
00059         }
00060 
00061                                 
00062 CSimple* CSimple::NewLC(TInt aVal)
00063         {
00064           // NewLC is enriched with a push to the cleanup stack
00065         CSimple* self=NewL(aVal);       
00066         CleanupStack::PushL(self);
00067         return self;
00068         }
00069 
00070                                 
00071 void CSimple::Display() 
00072         {
00073           // Display class data member on the console.
00074         _LIT(KFormat1,"Value=%d.\n");
00075         console->Printf(KFormat1,iVal);
00076         }
00077  
00078 CSimple::CSimple(TInt aVal) 
00079         : iVal(aVal)
00080         {}
00081 
00082 
00084 //
00085 // Main function called by E32
00086 //
00088 GLDEF_C TInt E32Main()
00089     {
00090           // Get cleanup stack
00091         CTrapCleanup* cleanup=CTrapCleanup::New();
00092 
00093           // Some more initialization, then do the example
00094         TRAPD(error,callExampleL());
00095           
00096           // callExampleL() should never leave.
00097         _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
00098         __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
00099 
00100           // destroy the cleanup stack
00101         delete cleanup;
00102         
00103           // return
00104         return 0;
00105     }
00106 
00107 
00109 //
00110 //
00111 //
00113 LOCAL_C void callExampleL() 
00114     {
00115           // Initialize and call the example code under cleanup stack.
00116         _LIT(KMsgExampleCode,"Symbian platform Example Code");
00117         console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00118           // Put console onto the cleanup stack.
00119         CleanupStack::PushL(console);
00120 
00121           // Mark for alloc heaven tool
00122         __UHEAP_MARK;                      
00123 
00124           // Perform the example function under the protection of a 
00125           // TRAP harness.
00126         TRAPD(error,doExampleL());
00127         
00128           // Test the example for alloc heaven
00129           __UHEAP_MARKEND;
00130 
00131           // 
00132         _LIT(KMsgOK,"ok");
00133         _LIT(KFormat2,"Overall example Trap Harness failed: leave code=%d");
00134         if (error)
00135                 console->Printf(KFormat2, error);
00136         else
00137                 console->Printf(KMsgOK);
00138           
00139           // Continue
00140         _LIT(KMsgPressAnyKey," [press any key]");
00141         console->Printf(KMsgPressAnyKey);
00142         console->Getch();
00143 
00144           // Remove the console object from the cleanupstack
00145           // and destroy it. 
00146         CleanupStack::PopAndDestroy();
00147     }
00148 
00149 
00151 //
00152 // Do the example
00153 //
00154 // Example checks the robustness of class on OOM
00156 void doExampleL()
00157         {
00158           // Start up the allocation failure tool to fail 
00159           // in the third cycle (arg=3 as there is 1 new
00160           // per cycle)
00161         __UHEAP_SETFAIL(RHeap::EDeterministic,3);
00162         
00163         for(TInt ii=1;ii<4;ii++)
00164                 {
00165                   // Display status information
00166                 _LIT(KFormat3,"Cycle %d.\n");
00167                 console->Printf(KFormat3,ii);
00168                   // Create new instance
00169                 CSimple* mySimpleExample = CSimple::NewL(2);
00170                   // Display the instance
00171                 mySimpleExample->Display();
00172                   // Destroy the instance
00173                 delete mySimpleExample;
00174                 }
00175         }
00176 
00177 
00178 

Generated by  doxygen 1.6.2