examples/Base/MemMan/Cleanup/SimpleOOM/SimpleOOM.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 Example checks the robustness of a simple class on Out Of Memory (OOM)
00029 NOTE: the structure of this example is different to standard E32 examples
00030 */
00031 
00032 
00033 
00034 #include <e32cons.h>
00035 
00036 
00037 
00038   // All messages written to this
00039 LOCAL_D CConsoleBase* console;
00040   
00041   // Function prototypes
00042 LOCAL_C void doExampleL();      
00043 LOCAL_C void callExampleL();
00044 
00045 
00047 //
00048 // -----> CSimple (definition)
00049 //
00051 class CSimple : public CBase
00052         {
00053 public :
00054         static CSimple* NewL(TInt aVal);
00055         static CSimple* NewLC(TInt aVal);
00056         void   Display();
00057 protected: 
00058         CSimple(TInt aVal);
00059 public: 
00060         TInt   iVal;
00061         };
00062 
00063 
00065 //
00066 // -----> CSimple (implementation)
00067 //
00069 CSimple* CSimple::NewL(TInt aVal) 
00070         {
00071           // NB The NewL function uses the C++ constructor mechanism.
00072         CSimple* self=new (ELeave) CSimple(aVal);
00073         return self;
00074         }
00075 
00076                                 
00077 CSimple* CSimple::NewLC(TInt aVal)
00078         {
00079           // NewLC is enriched with a push to the cleanup stack
00080         CSimple* self=NewL(aVal);       
00081         CleanupStack::PushL(self);
00082         return self;
00083         }
00084 
00085                                 
00086 void CSimple::Display() 
00087         {
00088           // Display class data member on the console.
00089         _LIT(KFormat1,"Value=%d.\n");
00090         console->Printf(KFormat1,iVal);
00091         }
00092  
00093 CSimple::CSimple(TInt aVal) 
00094         : iVal(aVal)
00095         {}
00096 
00097 
00099 //
00100 // Main function called by E32
00101 //
00103 GLDEF_C TInt E32Main()
00104     {
00105           // Get cleanup stack
00106         CTrapCleanup* cleanup=CTrapCleanup::New();
00107 
00108           // Some more initialization, then do the example
00109         TRAPD(error,callExampleL());
00110           
00111           // callExampleL() should never leave.
00112         _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
00113         __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
00114 
00115           // destroy the cleanup stack
00116         delete cleanup;
00117         
00118           // return
00119         return 0;
00120     }
00121 
00122 
00124 //
00125 //
00126 //
00128 LOCAL_C void callExampleL() 
00129     {
00130           // Initialize and call the example code under cleanup stack.
00131         _LIT(KMsgExampleCode,"Symbian platform Example Code");
00132         console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00133           // Put console onto the cleanup stack.
00134         CleanupStack::PushL(console);
00135 
00136           // Mark for alloc heaven tool
00137         __UHEAP_MARK;                      
00138 
00139           // Perform the example function under the protection of a 
00140           // TRAP harness.
00141         TRAPD(error,doExampleL());
00142         
00143           // Test the example for alloc heaven
00144           __UHEAP_MARKEND;
00145 
00146           // 
00147         _LIT(KMsgOK,"ok");
00148         _LIT(KFormat2,"Overall example Trap Harness failed: leave code=%d");
00149         if (error)
00150                 console->Printf(KFormat2, error);
00151         else
00152                 console->Printf(KMsgOK);
00153           
00154           // Continue
00155         _LIT(KMsgPressAnyKey," [press any key]");
00156         console->Printf(KMsgPressAnyKey);
00157         console->Getch();
00158 
00159           // Remove the console object from the cleanupstack
00160           // and destroy it. 
00161         CleanupStack::PopAndDestroy();
00162     }
00163 
00164 
00166 //
00167 // Do the example
00168 //
00169 // Example checks the robustness of class on OOM
00171 void doExampleL()
00172         {
00173           // Start up the allocation failure tool to fail 
00174           // in the third cycle (arg=3 as there is 1 new
00175           // per cycle)
00176         __UHEAP_SETFAIL(RHeap::EDeterministic,3);
00177         
00178         for(TInt ii=1;ii<4;ii++)
00179                 {
00180                   // Display status information
00181                 _LIT(KFormat3,"Cycle %d.\n");
00182                 console->Printf(KFormat3,ii);
00183                   // Create new instance
00184                 CSimple* mySimpleExample = CSimple::NewL(2);
00185                   // Display the instance
00186                 mySimpleExample->Display();
00187                   // Destroy the instance
00188                 delete mySimpleExample;
00189                 }
00190         }
00191 
00192 
00193 

Generated by  doxygen 1.6.2