Symbian3/SDK/Source/GUID-18EF9CFA-5080-5F89-89EC-C64897612D6B.dita
changeset 0 89d6a7a84779
equal deleted inserted replaced
-1:000000000000 0:89d6a7a84779
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-18EF9CFA-5080-5F89-89EC-C64897612D6B" xml:lang="en"><title>How
       
    13 to clean up non-CBase classes</title><shortdesc/><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <section id="GUID-DDB53FB1-7E70-45C6-93F8-76FBE03CA6FA"><title>Cleanup for a TAny*</title> <p>This example code shows cleanup
       
    15 for a <codeph>TAny*</codeph>, in this case a <codeph>TText</codeph>. </p> <codeblock id="GUID-62C1326A-2C9F-5F57-AA1C-EA557D0FF4FE" xml:space="preserve">TText* buffer=(TText*) User::Alloc(100*sizeof(TText)); 
       
    16 // create a buffer 
       
    17 CleanupStack::PushL(buffer);
       
    18 // push it to the cleanup stack: treated as TAny*
       
    19 TPtr8 bufferPtr(buffer,100);  // create a pointer to the buffer
       
    20 ...
       
    21 // use the buffer
       
    22 useBufferL(bufferPtr);
       
    23 ...
       
    24 // destroy the buffer on the cleanup stack
       
    25 CleanupStack::PopAndDestroy();  
       
    26 </codeblock> </section>
       
    27 <section id="GUID-B2DF0D27-9520-4B7D-A8BC-1B4B1A80A714"><title>Cleanup support for an R class</title> <p>This example code
       
    28 shows how to provide cleanup stack support for an <codeph>R</codeph> class.
       
    29 To do this, a <codeph>TCleanupItem</codeph> object must be constructed with
       
    30 a pointer to the object to clean up, and a pointer to a function that provides
       
    31 cleanup for that object. The most effective way to do this is to define a <codeph>TCleanupItem</codeph> cast
       
    32 operator in the class. </p> <codeblock id="GUID-FEFC12E2-0BE9-5AEF-89AE-E812651D8AD1" xml:space="preserve">// TCleanupItem operator for objects of this class
       
    33 RExampleClass::operator TCleanupItem()
       
    34  {
       
    35  return TCleanupItem(Cleanup,this);
       
    36  }</codeblock> <codeblock id="GUID-5D448C6A-C919-5251-9AC1-139A6F3703FC" xml:space="preserve">...</codeblock> <codeblock id="GUID-92812389-ADAF-554B-BF5E-AA515DF4BFB5" xml:space="preserve">// cleanup function for use by cleanup stack
       
    37 static void RExampleClass::Cleanup(TAny *aPtr)
       
    38  { 
       
    39  // Invoke the Close member on the RExampleClass at aPtr
       
    40  testConsole.Printf(_L("Doing cleanup.\n"));
       
    41  (static_cast&lt;RExampleClass*&gt;(aPtr))-&gt;Close();
       
    42  }
       
    43 
       
    44 // Show use
       
    45 void DoTheExampleL()
       
    46  {
       
    47  RExampleClass r;
       
    48  r.Open();
       
    49  // Because RExampleClass has an operator TCleanupItem()
       
    50  // pushing it is OK
       
    51  CleanupStack::PushL(r);
       
    52 
       
    53  // ...use r
       
    54  // possibly some operations that leave
       
    55 
       
    56  // PopAndDestroy() invokes RExampleClass::Cleanup()
       
    57  CleanupStack::PopAndDestroy();
       
    58  }
       
    59  </codeblock> <p><b>Notes</b> </p> <ul>
       
    60 <li id="GUID-E8C0A2EF-AD7D-55C9-9805-ADA6072C18F0"><p>The operator returns
       
    61 a <codeph>TCleanupItem</codeph> object which is constructed from a pointer
       
    62 to the static member function which performs the class’s cleanup processing,
       
    63 and a pointer to the object to be cleaned up. </p> </li>
       
    64 <li id="GUID-83B10F9E-E07F-59BD-A1A4-62795F6EFF19"><p>The static member function
       
    65 which provides the class’s cleanup processing must cast the <codeph>TAny*</codeph> pointer
       
    66 into a pointer of the class to be cleaned up. This allows the class member
       
    67 function to perform the cleanup </p> </li>
       
    68 <li id="GUID-61BAAE8E-C8B4-5034-B4D1-3E31F790B54C"><p> <codeph>CleanupStack::PopAndDestroy()</codeph> removes
       
    69 the item from the cleanupstack and invokes the cleanup function defined in
       
    70 the <codeph>TCleanupItem</codeph> object. </p> </li>
       
    71 <li id="GUID-53F43DCE-9260-5A71-9D59-AABD7916AE9E"><p>Remember that the <codeph>TCleanupItem</codeph> does <i>not</i> go
       
    72 onto the cleanupstack. the <codeph>TCleanupItem</codeph> is a container for
       
    73 the object that goes onto the cleanupstack. In this example, a pointer to
       
    74 the <codeph>RExampleClass</codeph> is put on the cleanupstack. </p> </li>
       
    75 </ul> </section>
       
    76 </conbody></concept>