libraries/iosrv/client/env.cpp
changeset 95 b3ffff030d5c
parent 0 7f656887cf89
--- a/libraries/iosrv/client/env.cpp	Tue Oct 26 15:36:30 2010 +0100
+++ b/libraries/iosrv/client/env.cpp	Thu Oct 28 16:54:54 2010 +0100
@@ -141,33 +141,37 @@
 	WaitLC();
 
 	HBufC** valPtr = iVars.Find(aKey);
-	HBufC* currentValue = NULL;
-	if (valPtr) currentValue = *valPtr;
 
 	if (valPtr == NULL && iParentEnv)
 		{
 		// If we don't have it, and we have a parent env, we should pass the request through to it.
 		iParentEnv->SetL(aKey, aValue);
 		}
+	else if (valPtr)
+		{
+		// The key is present in the hash.
+		HBufC* currentValue = *valPtr;
+		if (currentValue && (currentValue->Des().MaxLength() >= aValue.Length()))
+			{
+			// If the existing HBufC is big enough, simply copy.
+			// Note, currentValue will be NULL if this is the first time a 'local' variable is being set.
+			currentValue->Des().Copy(aValue);
+			}
+		else
+			{
+			// Otherwise allocate a new HBufC.
+			*valPtr = aValue.AllocL();
+			delete currentValue;
+			}
+		}
 	else
 		{
-		if (currentValue)
-			{
-			// If we already have a value in the hash, just update it if possible
-			TPtr ptr = currentValue->Des();
-			if (ptr.MaxLength() >= aValue.Length())
-				{
-				ptr.Copy(aValue);
-				CleanupStack::PopAndDestroy(); // Release lock
-				return;
-				}
-			}
+		// New key.
+		HBufC* newVal = aValue.AllocLC();
+		iVars.InsertL(aKey, newVal);
+		CleanupStack::Pop(newVal);
 		}
 	
-	HBufC* newVal = aValue.AllocLC();
-	iVars.InsertL(aKey, newVal);
-	delete currentValue;
-	CleanupStack::Pop(newVal);
 	CleanupStack::PopAndDestroy(); // Release lock
 	}