emailservices/emailframework/src/CFSMailClient.cpp
branchRCL_3
changeset 16 b5fbb9b25d57
parent 8 e1b6206813b4
child 20 efd4f1afd43e
--- a/emailservices/emailframework/src/CFSMailClient.cpp	Tue Apr 27 16:20:14 2010 +0300
+++ b/emailservices/emailframework/src/CFSMailClient.cpp	Tue May 11 15:57:15 2010 +0300
@@ -33,24 +33,28 @@
 {
     FUNC_LOG;
 
-	CFSMailClient* client = Instance();
-	if(!client)
-	{
-		client = new (ELeave) CFSMailClient();
-		CleanupStack:: PushL(client);
-		client->ConstructL(aConfiguration);
-		TInt err = Dll::SetTls(static_cast<TAny*>(client));
-		User::LeaveIfError(err);
-		}
-	else
-		{
-		CleanupStack:: PushL(client);
-		}
+    CFSMailClient* client = Instance();
+    if( !client )
+        {
+        client = new (ELeave) CFSMailClient();
+        // The new client needs to be deleted rather than closed, up until
+        // the TLS has been set properly.
+        CleanupStack::PushL( client );
+        client->ConstructL( aConfiguration );
+        TInt err = Dll::SetTls( static_cast<TAny*>( client ));
+        User::LeaveIfError( err );
+        // Now that the TLS has been set, we no longer want the new client
+        // to be deleted if there's a problem, so pop it off the cleanup
+        // stack (it will be pushed back on for closing below).
+        CleanupStack::Pop( client );
+        }
 
-	client->IncReferenceCount();
-	
-	return client;
-
+    // Increment reference count before calling CleanupClosePushL so that
+    // the reference count is correct if CleanupClosePushL leaves.
+    client->IncReferenceCount();
+    CleanupClosePushL( *client );
+    
+    return client;
 } 
 
 // -----------------------------------------------------------------------------