Mercurial > hg > orthanc-stone
comparison Applications/Platforms/WebAssembly/WebAssemblyViewport.cpp @ 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 |
---|---|
118 { | 118 { |
119 that_.RefreshCanvasSize(); | 119 that_.RefreshCanvasSize(); |
120 } | 120 } |
121 }; | 121 }; |
122 | 122 |
123 | |
124 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData) | 123 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData) |
125 { | 124 { |
126 LOG(TRACE) << __func__; | 125 LOG(TRACE) << __func__; |
127 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData); | 126 |
128 | 127 WebAssemblyViewport* that = |
129 if (that->compositor_.get() != NULL && | 128 WebAssemblyViewport::DereferenceObjectCookie(userData); |
130 that->controller_ /* should always be true */) | 129 |
131 { | 130 if (that != NULL) |
132 that->animationFrameCallbackIds_.clear(); | 131 { |
133 that->Paint(*that->compositor_, *that->controller_); | 132 if (that->compositor_.get() != NULL && |
134 } | 133 that->controller_ /* should always be true */) |
135 | 134 { |
135 that->Paint(*that->compositor_, *that->controller_); | |
136 } | |
137 } | |
138 else | |
139 { | |
140 LOG(INFO) << "WebAssemblyViewport::OnRequestAnimationFrame " | |
141 << "-- the WebAssemblyViewport is deleted and Paint will " | |
142 << "not be called"; | |
143 } | |
136 LOG(TRACE) << "Exiting: " << __func__; | 144 LOG(TRACE) << "Exiting: " << __func__; |
137 return true; | 145 return true; |
138 } | 146 } |
139 | 147 |
140 EM_BOOL WebAssemblyViewport::OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) | 148 EM_BOOL WebAssemblyViewport::OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) |
210 | 218 |
211 LOG(TRACE) << "Exiting: " << __func__; | 219 LOG(TRACE) << "Exiting: " << __func__; |
212 return true; | 220 return true; |
213 } | 221 } |
214 | 222 |
223 void* WebAssemblyViewport::GetObjectCookie() | |
224 { | |
225 if(objectCookie_ != NULL) | |
226 return objectCookie_; | |
227 | |
228 boost::shared_ptr<WebAssemblyViewport>* sharedFromThisPtr = | |
229 new boost::shared_ptr<WebAssemblyViewport>(); | |
230 | |
231 *sharedFromThisPtr = shared_from_this(); | |
232 | |
233 objectCookie_ = reinterpret_cast<void*>(sharedFromThisPtr); | |
234 | |
235 return objectCookie_; | |
236 } | |
237 | |
238 void WebAssemblyViewport::ReleaseObjectCookie(void* cookie) | |
239 { | |
240 WebAssemblyViewport* This = DereferenceObjectCookie(cookie); | |
241 | |
242 if (This != NULL) | |
243 This->objectCookie_ = NULL; | |
244 | |
245 boost::weak_ptr<WebAssemblyViewport>* weakThisPtr = | |
246 reinterpret_cast<boost::weak_ptr<WebAssemblyViewport>*>(cookie); | |
247 | |
248 delete weakThisPtr; | |
249 } | |
250 | |
251 WebAssemblyViewport* WebAssemblyViewport::DereferenceObjectCookie(void* cookie) | |
252 { | |
253 boost::weak_ptr<WebAssemblyViewport>* weakThisPtr = | |
254 reinterpret_cast<boost::weak_ptr<WebAssemblyViewport>*>(cookie); | |
255 | |
256 boost::shared_ptr<WebAssemblyViewport> sharedThis = weakThisPtr->lock(); | |
257 | |
258 return sharedThis.get(); | |
259 } | |
260 | |
215 void WebAssemblyViewport::Invalidate() | 261 void WebAssemblyViewport::Invalidate() |
216 { | 262 { |
217 long id = emscripten_request_animation_frame(OnRequestAnimationFrame, | 263 long id = emscripten_request_animation_frame(OnRequestAnimationFrame, |
218 reinterpret_cast<void*>(this)); | 264 GetObjectCookie()); |
219 animationFrameCallbackIds_.push_back(id); | 265 animationFrameCallbackIds_.push_back(id); |
220 } | 266 } |
221 | 267 |
222 void WebAssemblyViewport::FitForPrint() | 268 void WebAssemblyViewport::FitForPrint() |
223 { | 269 { |
257 canvasCssSelector_(canvasId), | 303 canvasCssSelector_(canvasId), |
258 #endif | 304 #endif |
259 interactor_(new DefaultViewportInteractor), | 305 interactor_(new DefaultViewportInteractor), |
260 enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents), | 306 enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents), |
261 canvasWidth_(0), | 307 canvasWidth_(0), |
262 canvasHeight_(0) | 308 canvasHeight_(0), |
309 objectCookie_(NULL) | |
263 { | 310 { |
264 } | 311 } |
265 | 312 |
266 void WebAssemblyViewport::PostConstructor() | 313 void WebAssemblyViewport::PostConstructor() |
267 { | 314 { |