examples/SFExamples/Example_RArray/ThingsNotToDo.cpp

00001 /*
00002 Copyright (c) 2002-2011 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 */ 
00029 #include "ThingsNotToDo.h"
00030 #include "SharedCode.h"
00031 
00032 // ---------------------------------------------------------
00033 // void AllocRArrayObjectsL()
00034 // This shows how its possible to leak memory if RArray
00035 // are put onto the heap.
00036 // ---------------------------------------------------------
00037 //
00038 LOCAL_C void AllocRArrayObjectsL()
00039         {
00040         // put the array on the stack
00041         // this goes in as a TAny* pointer with a granulatiry of 8
00042         RExampleArray* array = new (ELeave) RExampleArray;
00043         CleanupStack::PushL(array);
00044         
00045         // now add an item so the array allocates resources
00046         // internally
00047         TExampleStruct s = {1,2};
00048         User::LeaveIfError(array->Append(s));
00049         
00050         // Now if we leave the memory allocated for RExampleArray
00051         // gets deleted but 
00052         // a) its destructor will not be called
00053         // b) the resources allocated when we addded an item
00054         //    will not be cleaned up
00055         User::Leave(KErrGeneral);
00056         
00057         // This never gets called - its intentional!
00058         CleanupStack::PopAndDestroy(array);
00059         }
00060 
00061 // ---------------------------------------------------------
00062 // void UnalignedDataL()
00063 // Shows how to cause an alignment issue using RArray
00064 // This is typical use case that has come up and is a result
00065 // of the array elements not being the correct minimum size
00066 // ---------------------------------------------------------
00067 //
00068 LOCAL_C void UnalignedDataL()
00069         {
00070         TUint8 data[] = {1,2,3,4,5};
00071         
00072         // NOTE: No resource management as this code
00073         // will panic anyway.
00074         RArray<TUint8> array;
00075         
00076         // we pass in a reference not aligned
00077         // to a 32 bit address so it panics
00078         // with an alignment fault
00079         array.Append(data[3]);
00080         }
00081 
00082 // ---------------------------------------------------------
00083 // void ThingsNotToDoL()
00084 // Shows the things not do with RArrays
00085 // ---------------------------------------------------------
00086 //
00087 void ThingsNotToDoL()
00088         {
00089         // Create an RArray on the heap 
00090         AllocRArrayObjectsL();
00091         
00092         // misaligned data will cause a panic
00093         UnalignedDataL();
00094         }

Generated by  doxygen 1.6.2