Index: ui/gl/scoped_cgl.cc |
diff --git a/ui/gl/scoped_cgl.cc b/ui/gl/scoped_cgl.cc |
index 9fef3851c1ab17501f73cdddf5b4050c28131bdc..18eb9808de1dcef969ad43dedd7509954c6549fd 100644 |
--- a/ui/gl/scoped_cgl.cc |
+++ b/ui/gl/scoped_cgl.cc |
@@ -7,8 +7,14 @@ |
namespace gfx { |
-ScopedCGLSetCurrentContext::ScopedCGLSetCurrentContext(CGLContextObj context) |
- : previous_context_(CGLGetCurrentContext(), base::scoped_policy::RETAIN) { |
+ScopedCGLSetCurrentContext::ScopedCGLSetCurrentContext(CGLContextObj context) { |
+ CGLContextObj previous_context = CGLGetCurrentContext(); |
+ // It is possible for the previous context to have a zero reference count, |
+ // because making a context current does not increment the reference count. |
+ // In that case, do not restore the previous context. |
+ if (previous_context && CGLGetContextRetainCount(previous_context)) { |
+ previous_context_.reset(previous_context, base::scoped_policy::RETAIN); |
+ } |
CGLError error = CGLSetCurrentContext(context); |
DCHECK_EQ(error, kCGLNoError) << "CGLSetCurrentContext should never fail"; |
} |