diff Applications/Platforms/WebAssembly/WebAssemblyViewport.h @ 1621:575f512cdf48

Used a weak_ptr as a cookie to RequestAnimationFrame to prevent accessing a deleted viewport.
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 02 Nov 2020 20:45:04 +0100
parents 9a52bac0c2a7
children 74be0f498b08
line wrap: on
line diff
--- a/Applications/Platforms/WebAssembly/WebAssemblyViewport.h	Mon Nov 02 17:56:49 2020 +0100
+++ b/Applications/Platforms/WebAssembly/WebAssemblyViewport.h	Mon Nov 02 20:45:04 2020 +0100
@@ -62,6 +62,7 @@
     unsigned int                          canvasWidth_;
     unsigned int                          canvasHeight_;
     std::vector<long>                     animationFrameCallbackIds_;
+    void*                                 objectCookie_;
 
     static EM_BOOL OnRequestAnimationFrame(double time, void *userData);
     
@@ -100,6 +101,45 @@
 
     void PostConstructor();
 
+
+    /**
+     * This method can be called to retrieve a cookie that can be passed to 
+     * C-style callbacks that expect the object to be passed as a void*
+     * 
+     * This cookie is a resource and must be freed when it is guaranteed 
+     * not to be used anymore, with ReleaseObjectCookie
+     */
+    void* GetObjectCookie();
+
+    /**
+     * This static method can be used to dereference a cookie (i.e. retrieve 
+     * a pointer to the underlying object) that has been created with 
+     * WebAssemblyViewport::GetObjectCookie()
+     * 
+     * If this method returns NULL, it basically means that the 
+     * WebAssemblyViewport has already been deleted and that you should NOT 
+     * attempt to use it!
+     * 
+     * This method never releases the cookie (for other in-flight callbacks
+     * could possibly require to cookie to be valid)
+     * 
+     * If this method is called AFTER ReleaseObjectCookie has been called on
+     * the same cookie, the behavior is undefined and things will CERTAINLY
+     * go wrong.
+     * 
+     * NEVER call this method on a void* that has not been generated by the
+     * GetObjectCookie method of this class
+     */
+    static WebAssemblyViewport* DereferenceObjectCookie(void* cookie);
+
+    /**
+     * This method must be used when the object cookie will not be used anymore.
+     * 
+     * You must call it when you are certain that no entity will attempt to 
+     * dereference the cookie.
+     */
+    static void ReleaseObjectCookie(void* cookie);
+
     void RefreshCanvasSize();
 
     unsigned int GetCanvasWidth() const