examples/QtQuick/descriptors/otherbuffers_symbian.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation
00006 **
00007 **
00008 ** $QT_BEGIN_LICENSE:BSD$
00009 ** You may use this file under the terms of the BSD license as follows:
00010 **
00011 ** "Redistribution and use in source and binary forms, with or without
00012 ** modification, are permitted provided that the following conditions are
00013 ** met:
00014 **   * Redistributions of source code must retain the above copyright
00015 **     notice, this list of conditions and the following disclaimer.
00016 **   * Redistributions in binary form must reproduce the above copyright
00017 **     notice, this list of conditions and the following disclaimer in
00018 **     the documentation and/or other materials provided with the
00019 **     distribution.
00020 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00021 **     the names of its contributors may be used to endorse or promote
00022 **     products derived from this software without specific prior written
00023 **     permission.
00024 **
00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00036 ** $QT_END_LICENSE$
00037 **
00038 ****************************************************************************/
00039 
00040 
00041 
00042 // System Includes.
00043 #include <e32std.h>
00044 #include <e32base.h>
00045 
00047 #include "stringrenderer_symbian.h"
00048 #include "descriptorexamples_symbian.h"
00049 
00055     class TExample
00056         {
00057         public:
00058             TExample() : iNumber(0), iText() {}
00059             TExample(TInt aNumber, const TDesC &aText);
00060         public:
00061             TInt iNumber;
00062             TBuf<16> iText;
00063         };
00064 
00065     // render message and characteristics of TExample class
00066     // e.g. 'message: 10, "abcd"'
00067     LOCAL_C void ShowContents(const TDesC &aMsg,
00068                               const TExample aExample,
00069                               TPtr &aOutput );
00070 
00071     // render message and contents of given descriptor character data,
00072     // e.g. 'message: "abcd"'
00073     LOCAL_C void ShowContents(const TDesC &aMsg,
00074                               const TDesC &aTxt,
00075                               TPtr &aOutput);
00076 
00077     // render message and contents of given descriptor character data,
00078     // e.g. 'message: "abcd"'
00079     LOCAL_C void ShowContents(const TDesC &aMsg,
00080                               const TDesC8 &aTxt,
00081                               TPtr &aOutput);
00082 
00083     // render number of items in fifo and maximum number of
00084     // items the given fifo can store. e.g. 'fifo: size=1, max=5'.
00085     template <class T>
00086     LOCAL_C void ShowContents(CCirBuf<T> *aFIFO, TPtr &aOutput);
00087 
00088     // Render characteristics of flat dynamic buffer storing 8 bit characters.
00089     // The result is similar to this example:
00090     //
00091     //   CBufFlat: size=8, data @13984184=
00092     //   "abcdefgh"
00093     //
00094     LOCAL_C void ShowBuf(CBufFlat &aBuf, TDes &aOutput);
00095 
00096     // Render characteristics of segmented dynamic buffer storing 8 bit
00097     // characters. The result is similar to this example:
00098     //
00099     // CBufSeg: size=28, segments=
00100     //   "abcdefghij" @13984212
00101     //   "klmnopqrst" @13984248
00102     //   "uvwxyz01" @13984284
00103     //
00104     LOCAL_C void ShowBuf(CBufSeg &aBuf, TDes &aOutput);
00105 
00106     // Render characteristics of package buffer storing TExample object.
00107     // The result is similar to this example:
00108     //
00109     //  TPckgBuf @309000944, sizeof=52, storing
00110     //    TExample @309000952, sizeof=44, iNumber=1234567, iText="Hello!"
00111     //
00112     LOCAL_C void ShowContents(TPckgBuf<TExample> &aPackageBuf, TPtr &aOutput);
00113 
00114     // Render characteristics of package buffer pointer referring to TExample
00115     // object. The result is similar to this example:
00116     //
00117     //  TPckg @309000932, sizeof=12, referring to
00118     //    TExample @309001012, sizeof=44, iNumber=12345, iText="Hello!"
00119     //
00120     LOCAL_C void ShowContents(TPckg<TExample> &aPackage, TPtr &aOutput);
00121 
00122 // Texts for adding and removing operations
00123 _LIT( KAddedMsg, "\nAdded" );
00124 _LIT( KRemovedMsg, "\nRemoved" );
00125 _LIT8( KChars8, "abcdefghijklmnopqrstuvwxyz0123456789" );
00126 
00132 void CDescriptorExamples::CircularBuffersL()
00133     {
00134     TPtr output( iViewer->GetViewBuffer() );
00135     _LIT(KCircularBuffersTxt, "CircularBuffers:TText");
00136     RenderHeader( KCircularBuffersTxt, output );
00137 
00138     // -------------------------------------------------------------------------
00139     // This first example demonstrates how circular buffers can be used to
00140     // buffer character data.
00141     TBuf<10> deletedChars; // stores characters removed from fifo
00142     _LIT( KNumbersTxt, "0123456789" );
00143     // point to first character in character array stored in TLitC
00144     TText *charPtr = (TText*)KNumbersTxt().Ptr();
00145     CCirBuf<TText> *textFIFO;
00146 
00147     // construct fifo able to store up to ten characters:
00148     // contents of fifo is shown below (H=head, T=Tail).
00149     // fifo: data= "__________", count = 0
00150     //              ^
00151     //             H&T
00152     textFIFO = new (ELeave) CCirBuf<TText>();
00153     CleanupStack::PushL( textFIFO );
00154     textFIFO->SetLengthL( 10 ); // can store up to ten characters
00155 
00156     // Copy first 5 items (characters) to fifo
00157     // fifo: data= "01234_____", count = 5
00158     //              ^    ^
00159     //              T    H
00160     textFIFO->Add( charPtr, 5 ); // 01234
00161     ShowContents( KAddedMsg, KNumbersTxt().Left(5), output );
00162     ShowContents( textFIFO, output ); // size=5, max=10
00163 
00164     // remove three items from fifo (first three characters) and
00165     // store copies of them to deletedChars buffer.
00166     // fifo: data= "___34_____", count = 2
00167     //                 ^ ^
00168     //                 T H
00169     textFIFO->Remove( (TText*)deletedChars.Ptr(), 3 ); // chars "012"
00170     deletedChars.SetLength(3);
00171     ShowContents( KRemovedMsg, deletedChars, output );
00172     ShowContents( textFIFO, output ); // size=5, max=10
00173 
00174     // add 6 characters so the head rounds to the beginning.
00175     // fifo: data= "5__3401234", count = 8
00176     //               ^ ^
00177     //               H T
00178     textFIFO->Add( charPtr, 6 ); // 012345
00179     ShowContents( KAddedMsg, KNumbersTxt().Left(6), output );
00180     ShowContents( textFIFO, output ); // size=8, max=10
00181 
00182     // try to add 4 characters. Since there isn't enough space
00183     // only two characters are added since no more room available.
00184     // fifo: data= "5013401234", count = 10
00185     //                 ^
00186     //                H&T
00187     TInt charsAdded = textFIFO->Add( charPtr, 4 ); // 0123
00188     ShowContents( KAddedMsg, KNumbersTxt().Left(4), output );
00189     _LIT(KMsg, "But only %d characters was really added\n");
00190     output.AppendFormat( KMsg, charsAdded );
00191     ShowContents( textFIFO, output ); // size=10, max=10
00192 
00193     // remove eight items from fifo (now tail rounds to beginning)
00194     // fifo: data= "_01_______", count = 2
00195     //               ^ ^
00196     //               T H
00197     textFIFO->Remove( (TText*)deletedChars.Ptr(), 8 ); // chars "34012345"
00198     deletedChars.SetLength(8);
00199     ShowContents( KRemovedMsg, deletedChars, output );
00200     ShowContents( textFIFO, output ); // size=2, max=10
00201 
00202     // -------------------------------------------------------------------------
00203     // This example introduces how CCirBuf is used to store complex data types.
00204     // Since CCirBuf is templated its items can be any type.
00205     _LIT(KCircularBuffersEx, "CircularBuffers:TExample");
00206     RenderHeader( KCircularBuffersEx, output );
00207 
00208     // declare pointer to circular buffer (FIFO) that is able
00209     // to store TExample items.
00210     CCirBuf<TExample> *exampleFIFO;
00211     // when items are removed one by one, the copy of deleted item
00212     // is copied to this instance
00213     TExample removedItem;
00214 
00215     // Construct an instance of FIFO that can store up to
00216     // five Example class instances.
00217     exampleFIFO = new (ELeave) CCirBuf<TExample>();
00218     CleanupStack::PushL( exampleFIFO );
00219     exampleFIFO->SetLengthL( 5 );
00220 
00221     // Populate FIFO with three items and show contents
00222     // of added item and state of FIFO.
00223     _LIT(KOne, "one");
00224     TExample one(1, KOne);
00225     exampleFIFO->Add( &one );
00226     ShowContents( KAddedMsg, one, output );
00227     ShowContents( exampleFIFO, output ); // size=1, max=5
00228 
00229     _LIT(KTwo, "two");
00230     TExample two(2, KTwo);
00231     exampleFIFO->Add( &two );
00232     ShowContents( KAddedMsg, two, output );
00233     ShowContents( exampleFIFO, output ); // size=2, max=5
00234 
00235     _LIT(KThree, "three");
00236     TExample three(3, KThree);
00237     exampleFIFO->Add( &three );
00238     ShowContents( KAddedMsg, three, output );
00239     ShowContents( exampleFIFO, output ); // size=3, max=5
00240 
00241     // Remove item by item from FIFO and show contents of
00242     // removed item and the state of FIFO.
00243     // Remove() method takes a pointer to object where contents
00244     // of removed item is copied (binary copy)
00245     while( exampleFIFO->Count() > 0 )
00246         {
00247         exampleFIFO->Remove( &removedItem );
00248         ShowContents( KRemovedMsg, removedItem, output );
00249         ShowContents( exampleFIFO, output );
00250         }
00251 
00252     iViewer->UpdateView();
00253     CleanupStack::PopAndDestroy(2); // exampleFIFO, textFIFO
00254     }
00261 void CDescriptorExamples::FlatDynamicBuffersL()
00262     {
00263     TPtr output( iViewer->GetViewBuffer() );
00264     _LIT(KFlatDynamicBuffers, "FlatDynamicBuffers");
00265     RenderHeader( KFlatDynamicBuffers, output );
00266 
00267     // -------------------------------------------------------------------------
00268     // When content (8 bit characters) are added to container, it allocates the
00269     // memory automatically. In the first steps it can extend the existing heap
00270     // cell when more space is needed.
00271     //
00272     // However, later in the example a HBufC is allocated from heap to prevent
00273     // heap cell extending. When more data is appended to buffer, a new heap
00274     // cell is allocated from somewhere else, existing content is copied to new
00275     // cell, new content to be appended is added after it and finally the
00276     // existing heap cell is deallocated.
00277     //
00278     // The contents of buffer is shown after each character insertion. The
00279     // location of data (memory address of heap cell) doesn't change until the
00280     // fourth insertion because heap cell can't extend anymore.
00281 
00282     // construct a flat dynamic buffer that will automatically extend its heap
00283     // cell size with 10 bytes when more space is needed.
00284     CBufFlat *buf = CBufFlat::NewL( 10 );
00285     CleanupStack::PushL(buf);
00286 
00287     // this variable is used to refer a portion of characters in KChars8
00288     TPtrC8 charsToInsert(NULL, 0);
00289 
00290     charsToInsert.Set( KChars8().Mid( 0, 8 ) ); // "abcdefgh"
00291     buf->InsertL( 0, charsToInsert );
00292     ShowContents( KAddedMsg, charsToInsert, output );
00293     ShowBuf( *buf, output );
00294 
00295     charsToInsert.Set( KChars8().Mid( 8, 8 ) ); // "ijklmnop"
00296     buf->InsertL( 8, charsToInsert );
00297     ShowContents( KAddedMsg, charsToInsert, output );
00298     ShowBuf( *buf, output );
00299 
00300     charsToInsert.Set( KChars8().Mid( 16, 12 ) ); // "ijklmnop"
00301     buf->InsertL( 16, charsToInsert );
00302     ShowContents( KAddedMsg, charsToInsert, output );
00303     ShowBuf( *buf, output );
00304 
00305     // this object is allocated just after the flat dynamic buffers heap
00306     // cell preventing it to extend its cell size when more space is needed.
00307     HBufC *tmpBuf = HBufC::NewL( 50 );
00308     CleanupStack::PushL( tmpBuf );
00309     tmpBuf->Length(); // remove compiler warning that variable is not used
00310 
00311     // appending this data to buffer requires heap cell to extend.
00312     // tmpBuf prevents heap cell to extend so new cell has to be
00313     // allocated, original data copied to there and existing cell
00314     // to be freed.
00315     charsToInsert.Set( KChars8().Mid( 4, 20 ) ); // "efghijklmnopqrstuvwx"
00316     buf->InsertL(28, charsToInsert );
00317     ShowContents( KAddedMsg, charsToInsert, output );
00318     ShowBuf( *buf, output );
00319 
00320     iViewer->UpdateView();
00321     CleanupStack::PopAndDestroy(2); // tmpBuf, buf
00322     }
00328 void CDescriptorExamples::SegmentedDynamicBuffersL()
00329     {
00330     TPtr output( iViewer->GetViewBuffer() );
00331     _LIT(KSegmentedDynamicBuffers, "SegmentedDynamicBuffers");
00332     RenderHeader( KSegmentedDynamicBuffers, output );
00333 
00334     // Allocate a empty buffer with granularity of 10
00335     CBufSeg* buf = CBufSeg::NewL(10);
00336     CleanupStack::PushL( buf );
00337 
00338     TPtrC8 charsToInsert(NULL, 0);
00339 
00340     // append "abcdefgh"
00341     // All eight characters fit to first segment
00342     charsToInsert.Set( KChars8().Mid(0, 8) );
00343     buf->InsertL(0, charsToInsert );
00344     ShowContents( KAddedMsg, charsToInsert, output );
00345     ShowBuf( *buf, output );
00346 
00347     // append "ijklmnop"
00348     // first two characters go to the first segment and
00349     // the rest six to second one.
00350     charsToInsert.Set( KChars8().Mid(8, 8) );
00351     buf->InsertL(8, charsToInsert );
00352     ShowContents( KAddedMsg, charsToInsert, output );
00353     ShowBuf( *buf, output );
00354 
00355     // append "qrstuvwxyz01"
00356     // first four characters go to the second segment and
00357     // the rest eight to third one.
00358     charsToInsert.Set( KChars8().Mid(16, 12) );
00359     buf->InsertL(16, charsToInsert );
00360     ShowContents( KAddedMsg, charsToInsert, output );
00361     ShowBuf( *buf, output );
00362 
00363     // append "efghijklmnopqrstuvwx"
00364     // first two characters go to the third segment. Next ten to
00365     // fourth segment and rest eight to fifth segment.
00366     charsToInsert.Set( KChars8().Mid(4, 20) );
00367     buf->InsertL(28, charsToInsert );
00368     ShowContents( KAddedMsg, charsToInsert, output );
00369     ShowBuf( *buf, output );
00370 
00371     // delete a portion from the segmented buffer.
00372     //
00373     // An exmaple run shows that segments before deletion are
00374     //
00375     // CBufSeg: size=48, segments=
00376     //   "abcdefghij" @13984212
00377     //   "klmnopqrst" @13984248
00378     //   "uvwxyz01ef" @13984284
00379     //   "ghijklmnop" @13984356
00380     //   "qrstuvwx" @13984320
00381     //
00382     // and after deletion there are segments:
00383     //
00384     // CBufSeg: size=24, segments=
00385     //   "abc" @13984212
00386     //   "1ef" @13984284
00387     //   "ghijklmnop" @13984356
00388     //   "qrstuvwx" @13984320
00389     //
00390     // From this example it can be seen that modifying segment
00391     // buffer can result segments not fully filled. That's why
00392     // direct access thought pointer is difficult since the
00393     // segment sizes varies.
00394     buf->Delete(3, 24);
00395     _LIT(KMsg1, "\nDeleted 24 bytes from index 4\n");
00396     output.Append( KMsg1 );
00397     ShowBuf( *buf, output );
00398 
00399     // Replace character at even index with 'X'
00400     //
00401     // Since data is splitted to segments that may contain variable
00402     // number of character data it would be quite difficult to alter
00403     // data with pointer access. That's why the easiest way to alter
00404     // data is to copy it to fixed sized buffer, modify the copy and
00405     // write modified content over original data.
00406     //
00407     // Since the segmented buffer could be quite long in real problem
00408     // it is better to get used to modify the content in pieces.
00409     //
00410     // Example run produces:
00411     //
00412     // CBufSeg: size=24, segments=
00413     //   "aXc" @13984212
00414     //   "XeX" @13984284
00415     //   "gXiXkXmXoX" @13984356
00416     //   "qXsXuXwX" @13984320
00417     //
00418     TBuf8<20> tmpBuf;
00419     for( TInt i=0; i < buf->Size(); i += tmpBuf.MaxLength() )
00420     {
00421         TInt charsToCopy = buf->Size() - i; // Chars left
00422 
00423         // do not try to read more than tmpBuf can hold
00424         if( charsToCopy > tmpBuf.MaxLength() )
00425             {
00426             charsToCopy = tmpBuf.MaxLength();
00427             }
00428 
00429         // copy data from segmented buffer to descriptor
00430         buf->Read(i, tmpBuf, charsToCopy);
00431 
00432         // Change character in descriptor at even index
00433         for(TInt j=0; j<charsToCopy; j++)
00434             {
00435             if( j % 2 != 0 )
00436                 {
00437                     tmpBuf[j] = 'X';
00438                 }
00439             }
00440 
00441         // write modified content to the same location it was read
00442         buf->Write(i, tmpBuf, charsToCopy);
00443     }
00444     _LIT(KMsg2, "\nReplaced characters at even index with 'X'\n");
00445     output.Append( KMsg2 );
00446     ShowBuf( *buf, output );
00447 
00448     // lets compress the data to minimize heap usage: partially
00449     // filled segments are fullfilled and segments no more
00450     // needed are deallocated.
00451     //
00452     // Example run produces:
00453     //
00454     // CBufSeg: size=24, segments=
00455     //   "aXcXeXgXiX" @13984212
00456     //   "kXmXoXqXsX" @13984356
00457     //   "uXwX" @13984320
00458     //
00459     // (first and second segments were fullfilled. There didn't
00460     //  exist any data in fourth segment so if was deleted)
00461     //
00462     buf->Compress();
00463     _LIT(KCompressedBuf, "\nCompressed buffer\n");
00464     output.Append( KCompressedBuf );
00465     ShowBuf( *buf, output );
00466 
00467     iViewer->UpdateView();
00468     CleanupStack::PopAndDestroy(1);
00469     }
00470 
00476 void CDescriptorExamples::PackageBuffers()
00477     {
00478     TPtr output( iViewer->GetViewBuffer() );
00479     // ------------------------------------------------------------
00480     // This first example demonstrates how package buffer is used to
00481     // pack a value class inside.
00482     _LIT(KPackageBuffers1, "PackageBuffers:TPckgBuf");
00483     RenderHeader( KPackageBuffers1, output );
00484 
00485     // construct package buffer that does store one instance of TExample
00486     // instance. The insnce inside is empty since constructor of package
00487     // buffer does call also the constructor of capsulated object
00488     // (constructor of TExample in that case that sets fields to empty).
00489     TPckgBuf<TExample> pckgBuf;
00490     _LIT(KMsg3, "nCreated package buffer that stores an empty (by default) TExample object\n");
00491     output.Append( KMsg3 );
00492 
00493     ShowContents( pckgBuf, output );
00494 
00495     // modify TExample inside package buffer.
00496     //
00497     // To get access to the instance, operator () is used to get
00498     // reference.
00499     TExample &exampleRef = pckgBuf();
00500     exampleRef.iNumber = 1234567;
00501     _LIT(KMsg4, "\nModified iNumber of TExample inside package buffer\n");
00502     output.Append( KMsg4 );
00503     ShowContents( pckgBuf, output );
00504 
00505     // modify TExample inside package buffer - using existing reference
00506     _LIT(KHello, "Hello!");
00507     exampleRef.iText.Copy( KHello );
00508     _LIT(KMsg5, "\nModified iText of TExample inside package buffer\n");
00509     output.Append( KMsg5 );
00510     ShowContents( pckgBuf, output );
00511 
00512     // ------------------------------------------------------------
00513     // This second example demonstrates how package buffer pointer is
00514     // used to refer contenst of value class somewhere in memory.
00515     _LIT(KPackageBuffers2, "PackageBuffers:TPckg");
00516     RenderHeader( KPackageBuffers2, output );
00517 
00518     TExample example;
00519     TPckg<TExample> pckg( example );
00520     _LIT(KMsg6, "\nCreated package buffer that refers to an empty TExample object\n");
00521     output.Append( KMsg6 );
00522     ShowContents(pckg, output);
00523 
00524     // modify contents the example object. Showing contents of package
00525     // buffer reveals that package buffer did refer to our external
00526     // instance.
00527     example.iNumber = 12345;
00528     example.iText.Copy( KHello );
00529     _LIT(KMsg7, "\nCreated package buffer that refers to an empty TExample object\n");
00530     output.Append( KMsg7 );
00531     ShowContents(pckg, output);
00532 
00533     iViewer->UpdateView();
00534     }
00535 
00536 void CDescriptorExamples::RBufDemonstrations()
00537     {
00538     // RBuf::CreateL demo
00539     RBuf modifiableBuf;
00540     modifiableBuf.CreateL(12);
00541     ASSERT(modifiableBuf.Length()==0);
00542     ASSERT(modifiableBuf.MaxLength()==12);
00543     modifiableBuf.Close();
00544 
00545     // RBuf::CreateMaxL  demo
00546     modifiableBuf.CreateMaxL(12);
00547     ASSERT(modifiableBuf.Length()==12);
00548     ASSERT(modifiableBuf.MaxLength()==12);
00549     modifiableBuf.Close();
00550 
00551     // RBuf::CreateL passing in a descriptor
00552     _LIT(KHelloWorld, "Hello World");
00553     modifiableBuf.CreateL(KHelloWorld());
00554     ASSERT(modifiableBuf.Length()==11);
00555     ASSERT(modifiableBuf.MaxLength()==11);
00556     modifiableBuf.Close();
00557 
00558     // RBuf::CreateL passing in a descriptor and a maximum length
00559     modifiableBuf.CreateL(KHelloWorld(), 15);
00560     ASSERT(modifiableBuf.Length()==11);
00561     ASSERT(modifiableBuf.MaxLength()==15);
00562     modifiableBuf.Close();
00563 
00564     // RBuf::CreateL and ReAllocL & modifiable descriptor base class methods
00565     _LIT(KHello, "Hello");
00566     _LIT(KWorld, " World");
00567     modifiableBuf.CreateL(5);
00568     modifiableBuf.Copy(KHello());
00569     modifiableBuf.CleanupClosePushL(); // Push onto cleanup stack for leave safety
00570     modifiableBuf.ReAllocL(11);
00571     modifiableBuf.Append(KWorld);
00572     CleanupStack::PopAndDestroy(); // Calls modifiableBuf.Close()
00573 
00574     //RBuf::Assign leaks easily.
00575     //If you have memory allocated in the RBuf and you call Assign it leaks.
00576     //You should always call Close() before calling Assign()
00577     //The same goes with Create functions, if there's memory allocated it leaks.
00578     //modifiableBuf.CreateL(1);    
00579     //modifiableBuf.Close();
00580     HBufC* hBuf = KHello().AllocL();
00581     modifiableBuf.Assign(hBuf); //Take ownership of hBuf memory
00582     ASSERT(modifiableBuf.Length()==5);
00583     modifiableBuf.Close();
00584 
00585     //Assignments demo
00586     RBuf myRBuf1;
00587     RBuf myRBuf2;
00588     HBufC* myHBufC = HBufC::NewL(20);
00589     myRBuf1.Assign(myHBufC); //Take ownership of heap memory
00590     myRBuf2.Assign(myRBuf1); //Take ownership of another RBuf
00591     myRBuf2.Close();
00592     
00593     //Assign, ReAllocL and Append
00594     TUint16* ptr = static_cast<TUint16*> (User::AllocL(5*sizeof(TText)));    
00595     modifiableBuf.Assign(ptr,5);
00596     ASSERT(modifiableBuf.Length()==0);
00597     modifiableBuf.Copy(KHello()); //Copying any more would panic
00598     //modifiableBuf.Append(KWorld);//This  would cause a panic
00599 
00600     modifiableBuf.CleanupClosePushL(); //Push onto cleanup stack for leave safety
00601     modifiableBuf.ReAllocL(12);
00602     modifiableBuf.Append(KWorld);
00603     CleanupStack::PopAndDestroy(); //Calls modifiableBuf.Close() 
00604     }
00605 
00611 TExample::TExample(TInt aNumber, const TDesC &aText)
00612     {
00613         iNumber = aNumber;
00614         iText.Copy(aText.Left( iText.MaxLength() ) );
00615     }
00616 
00622 LOCAL_C void ShowContents(const TDesC& aMsg,
00623                           const TExample aExample,
00624                           TPtr &aOutput)
00625     {
00626     _LIT( KFormat, "%S: %d, \"%S\"\n" );
00627     aOutput.AppendFormat( KFormat, &aMsg, aExample.iNumber, &aExample.iText );
00628     }
00629 
00630 LOCAL_C void ShowContents(const TDesC& aMsg, const TDesC &aTxt, TPtr &aOutput)
00631     {
00632     _LIT( KFormat, "%S: \"%S\"\n" );
00633     aOutput.AppendFormat( KFormat, &aMsg, &aTxt );
00634     }
00635 
00636 LOCAL_C void ShowContents(const TDesC& aMsg, const TDesC8 &aTxt, TPtr &aOutput)
00637     {
00638     TBuf<128> buf;
00639     buf.Copy(aTxt.Left(buf.MaxLength()));
00640     _LIT( KFormat, "%S: \"%S\"\n" );
00641     aOutput.AppendFormat( KFormat, &aMsg, &buf );
00642     }
00643 
00644 template <class T>
00645 LOCAL_C void ShowContents(CCirBuf<T> *aFIFO, TPtr &aOutput)
00646     {
00647     _LIT( KFIFOFormat, "fifo: size=%d, max=%d\n" );
00648     aOutput.AppendFormat( KFIFOFormat, aFIFO->Count(), aFIFO->Length() );
00649     }
00650 
00651 LOCAL_C void ShowBuf(CBufFlat &aBuf, TDes &aOutput)
00652     {
00653     TPtrC8 data = aBuf.Ptr(0);
00654     _LIT(KFormatMsg, "CBufFlat: size=%d, data @%d=\n\"");
00655     aOutput.AppendFormat( KFormatMsg, aBuf.Size(), data.Ptr() );
00656     Append(data, aOutput);
00657     _LIT(KFormat, "\"\n");
00658     aOutput.Append(KFormat);
00659     }
00660 
00661 LOCAL_C void ShowBuf(CBufSeg &aBuf, TDes &aOutput)
00662     {
00663     _LIT(KFormatMsg, "CBufSeg: size=%d, segments=\n");
00664     aOutput.AppendFormat( KFormatMsg, aBuf.Size() );
00665     TInt pos = 0;
00666     _LIT(KFormat1, "  \"");
00667     _LIT(KFormat2, "\" @%d\n");
00668     while( pos < aBuf.Size() )
00669         {
00670         TPtrC8 ptr = aBuf.Ptr(pos);
00671         aOutput.Append( KFormat1 );
00672         Append(ptr, aOutput);
00673         aOutput.AppendFormat( KFormat2, ptr.Ptr() );
00674         pos += ptr.Length();
00675         }
00676     }
00677 
00678 LOCAL_C void ShowContents(TPckgBuf<TExample> &aPackageBuf, TPtr &aOutput)
00679     {
00680     _LIT(KFormat, "TPckgBuf @%d, sizeof=%d, storing\n  TExample @%d, sizeof=%d, iNumber=%d, iText=\"%S\"\n");
00681     aOutput.AppendFormat( KFormat, &aPackageBuf, sizeof(aPackageBuf), &aPackageBuf(), sizeof(aPackageBuf()), aPackageBuf().iNumber, &aPackageBuf().iText );
00682     }
00683 
00684 LOCAL_C void ShowContents(TPckg<TExample> &aPackage, TPtr &aOutput)
00685     {
00686     _LIT(KFormat, "TPckg @%d, sizeof=%d, referring to\n  TExample @%d, sizeof=%d, iNumber=%d, iText=\"%S\"\n");
00687     aOutput.AppendFormat( KFormat, &aPackage, sizeof(aPackage), &aPackage(), sizeof(aPackage()), aPackage().iNumber, &aPackage().iText );
00688     }
00689 

Generated by  doxygen 1.6.2