Bug 1286459 - Use WeakPtr for deferred references. - r=jrmuizel draft
authorJeff Gilbert <jgilbert@mozilla.com>
Mon, 18 Jul 2016 18:27:10 -0700
changeset 389308 1d6361c010a13ef8d1f390c49c54b1e61c146abc
parent 389307 a2b2066c452905d0c92f3703efdf3a5df1a6161e
child 525720 c926f4d40b1e37e29954f60d53944e7bb353c729
push id23368
push userbmo:jgilbert@mozilla.com
push dateTue, 19 Jul 2016 01:27:36 +0000
reviewersjrmuizel
bugs1286459
milestone50.0a1
Bug 1286459 - Use WeakPtr for deferred references. - r=jrmuizel MozReview-Commit-ID: IrDMIEy5fd1
dom/canvas/WebGLTimerQuery.cpp
dom/canvas/WebGLTimerQuery.h
--- a/dom/canvas/WebGLTimerQuery.cpp
+++ b/dom/canvas/WebGLTimerQuery.cpp
@@ -58,18 +58,25 @@ WebGLContext*
 WebGLTimerQuery::GetParentObject() const
 {
   return mContext;
 }
 
 void
 WebGLTimerQuery::QueueAvailablity()
 {
-  RefPtr<WebGLTimerQuery> self = this;
-  NS_DispatchToCurrentThread(NS_NewRunnableFunction([self] { self->mCanBeAvailable = true; }));
+  WeakPtr<WebGLTimerQuery> weakPtr = this;
+
+  const auto fnMakeAvailable = [weakPtr]() { // Copy the WeakPtr.
+    if (weakPtr) {
+      weakPtr->mCanBeAvailable = true;
+    }
+  };
+
+  NS_DispatchToCurrentThread(NS_NewRunnableFunction(fnMakeAvailable));
 }
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLTimerQuery)
 
 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLTimerQuery, AddRef)
 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLTimerQuery, Release)
 
 } // namespace mozilla
--- a/dom/canvas/WebGLTimerQuery.h
+++ b/dom/canvas/WebGLTimerQuery.h
@@ -13,28 +13,30 @@
 
 namespace mozilla {
 
 class WebGLTimerQuery final
   : public nsWrapperCache
   , public WebGLRefCountedObject<WebGLTimerQuery>
   , public LinkedListElement<WebGLTimerQuery>
   , public WebGLContextBoundObject
+  , public SupportsWeakPtr<WebGLTimerQuery>
 {
   friend class WebGLExtensionDisjointTimerQuery;
 
 public:
   const GLenum mGLName;
 protected:
   GLenum mTarget;
   bool mCanBeAvailable;
 
 public:
   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLTimerQuery)
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLTimerQuery)
+  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLTimerQuery)
 
   static WebGLTimerQuery* Create(WebGLContext* webgl);
 
 private:
   explicit WebGLTimerQuery(WebGLContext* webgl, GLuint name);
   ~WebGLTimerQuery();
 
   void Delete();