comparison 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
comparison
equal deleted inserted replaced
1620:1151e25d7311 1621:575f512cdf48
60 std::unique_ptr<IViewportInteractor> interactor_; 60 std::unique_ptr<IViewportInteractor> interactor_;
61 bool enableEmscriptenMouseEvents_; 61 bool enableEmscriptenMouseEvents_;
62 unsigned int canvasWidth_; 62 unsigned int canvasWidth_;
63 unsigned int canvasHeight_; 63 unsigned int canvasHeight_;
64 std::vector<long> animationFrameCallbackIds_; 64 std::vector<long> animationFrameCallbackIds_;
65 void* objectCookie_;
65 66
66 static EM_BOOL OnRequestAnimationFrame(double time, void *userData); 67 static EM_BOOL OnRequestAnimationFrame(double time, void *userData);
67 68
68 static EM_BOOL OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData); 69 static EM_BOOL OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData);
69 70
97 */ 98 */
98 WebAssemblyViewport(const std::string& canvasId, 99 WebAssemblyViewport(const std::string& canvasId,
99 bool enableEmscriptenMouseEvents); 100 bool enableEmscriptenMouseEvents);
100 101
101 void PostConstructor(); 102 void PostConstructor();
103
104
105 /**
106 * This method can be called to retrieve a cookie that can be passed to
107 * C-style callbacks that expect the object to be passed as a void*
108 *
109 * This cookie is a resource and must be freed when it is guaranteed
110 * not to be used anymore, with ReleaseObjectCookie
111 */
112 void* GetObjectCookie();
113
114 /**
115 * This static method can be used to dereference a cookie (i.e. retrieve
116 * a pointer to the underlying object) that has been created with
117 * WebAssemblyViewport::GetObjectCookie()
118 *
119 * If this method returns NULL, it basically means that the
120 * WebAssemblyViewport has already been deleted and that you should NOT
121 * attempt to use it!
122 *
123 * This method never releases the cookie (for other in-flight callbacks
124 * could possibly require to cookie to be valid)
125 *
126 * If this method is called AFTER ReleaseObjectCookie has been called on
127 * the same cookie, the behavior is undefined and things will CERTAINLY
128 * go wrong.
129 *
130 * NEVER call this method on a void* that has not been generated by the
131 * GetObjectCookie method of this class
132 */
133 static WebAssemblyViewport* DereferenceObjectCookie(void* cookie);
134
135 /**
136 * This method must be used when the object cookie will not be used anymore.
137 *
138 * You must call it when you are certain that no entity will attempt to
139 * dereference the cookie.
140 */
141 static void ReleaseObjectCookie(void* cookie);
102 142
103 void RefreshCanvasSize(); 143 void RefreshCanvasSize();
104 144
105 unsigned int GetCanvasWidth() const 145 unsigned int GetCanvasWidth() const
106 { 146 {