back port upstream patch

This patch is from Chromium project. It's used to handle potential issue
in scoped_refptr.

This is upstream patch porting and the original link is:
http://codereview.chromium.org/9021020/
unittest change was not included.

Change-Id: I14ce3df0653ff29713f38eceaf07e9b409cfaedc
Author: Fengwei Yin <fengwei.yin@intel.com>
Singed-off-by: Shuo Gao <shuo.gao@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Author-tracking-BZ: 37500
diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h
index 1207ed4..e35264b 100644
--- a/base/memory/ref_counted.h
+++ b/base/memory/ref_counted.h
@@ -260,9 +260,10 @@
     // AddRef first so that self assignment should work
     if (p)
       p->AddRef();
-    if (ptr_ )
-      ptr_ ->Release();
+    T* old_ptr = ptr_;
     ptr_ = p;
+    if (old_ptr)
+      old_ptr ->Release();
     return *this;
   }