diff -r 43e37759235e -r 51a74ef9ed63 Symbian3/SDK/Source/GUID-69D916D3-ED05-58DA-BA42-CE4D7E4F6482.dita --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/SDK/Source/GUID-69D916D3-ED05-58DA-BA42-CE4D7E4F6482.dita Wed Mar 31 11:11:55 2010 +0100 @@ -0,0 +1,81 @@ + + + + + +Automatic +Resource Management Class Templates TutorialThe LCleanedupX class provide a means of automatically +cleaning up local variables on scope exit. Variants are provided for cleaning +up pointers, references, handles and arrays, as well as generic cleanup items. +The user is able to define a cleanup strategy to clean up the resource on +scope exit. +

Before beginning you must know the following:

    +
  • Clean-up Stratergy: Cleanup +strategies can be specified as an optional template parameter of the class +templates for automatic resource management.

  • +
+ +Declaring an LCleanedupX object +

An LCleanedupX object is declared using one of the +class template constructors. The type to be managed is declared as the first +template parameter. An optional second template parameter can be declared +to implement a non-default cleanup strategy.

An example code snippet +for declaring LCleanedupX class is shown below:

class LCleanedupPtr + { + CTicker* ticker1 = new(ELeave) CTicker; + LCleanedupPtr CManagedUserTwoPhase one(CManagedUserTwoPhase::NewL(ticker1)); + + CTicker* ticker2 = new(ELeave) CTicker; + LCleanedupPtr CManagedUserSinglePhase two(CManagedUserSinglePhase::NewL(ticker2)); + } +
+
+Disabling Automatic Cleanup +

An object that is being managed by an LCleanedupX class +can be unmanaged by calling the Unmanage() method of the +LCleanedupX class.

The managing object can be accessed using the . +operator as in the call to Unmanage(). The Unmanage() disables +cleanup and yields the previously managed pointer so that it can be safely +returned.

An example code snippet is given below:

+ static CStringUserTwoPhase* NewL(const TDesC& aName) + { + LCleanedupPtr CStringUserTwoPhase self(new(ELeave) CStringUserTwoPhase); + self->ConstructL(aName); + return self.Unmanage(); + } +
+
+Accessing the managed object +

The managed object can be accessed using the -> operator as in the +call to ConstructL()

self->ConstructL(aName);
+
+Passing the managed object to a function +

The managed object can be passed to a function by dereferencing the +managing object. An example code snippet is given below:

+void RegisterTicker(CTicker& aTicker) + { + (void)aTicker; + } + +

The RegisterTicker function defined above +takes a CTicker& argument. If we have a LCleanedupPtr<CTicker> as +defined below, we can pass the CTicker object to the function +by dereferencing as shown below in the code snippet:

+LCleanedupPtr CTicker t(new(ELeave) CTicker); + +RegisterTicker(*t); // Takes a CTicker& +
+
+
+
+Automatic +Resource Management +Automatic +Resource Management Tutorial +
\ No newline at end of file