Skip to content

Commit 2c0658f

Browse files
committed
Fix memory corruption
The previous implementation was adding the parameters to the function call after posting the event causing a random crash or memory corruption. Adding a function parameter to the createEvent function to be called before posting the event fixes the problem. Change-Id: I924cfe490372091afc6219d097d8c6bf17a43045 Reviewed-by: Edward Welbourne <[email protected]> Reviewed-by: Jesus Fernandez <[email protected]>
1 parent d995226 commit 2c0658f

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/plugins/platforms/webgl/qwebglcontext.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ struct GLFunction
309309
};
310310

311311
template<const GLFunction *Function>
312-
static QWebGLFunctionCall *createEvent(bool wait)
312+
static QWebGLFunctionCall *createEventImpl(bool wait)
313313
{
314314
auto context = QOpenGLContext::currentContext();
315315
Q_ASSERT(context);
@@ -319,23 +319,46 @@ static QWebGLFunctionCall *createEvent(bool wait)
319319
if (!clientData || !clientData->socket
320320
|| clientData->socket->state() != QAbstractSocket::ConnectedState)
321321
return nullptr;
322-
auto pointer = new QWebGLFunctionCall(Function->remoteName, handle->currentSurface(), wait);
323-
if (wait)
324-
QWebGLContextPrivate::waitingIds.insert(pointer->id());
325-
QCoreApplication::postEvent(QWebGLIntegrationPrivate::instance()->webSocketServer, pointer);
326-
return pointer;
322+
return new QWebGLFunctionCall(Function->remoteName, handle->currentSurface(), wait);
323+
}
324+
325+
static void postEventImpl(QWebGLFunctionCall *event)
326+
{
327+
if (event->isBlocking())
328+
QWebGLContextPrivate::waitingIds.insert(event->id());
329+
QCoreApplication::postEvent(QWebGLIntegrationPrivate::instance()->webSocketServer, event);
330+
}
331+
332+
template<const GLFunction *Function, class... Ts>
333+
static QWebGLFunctionCall *createEventAndPostImpl(bool wait, Ts&&... arguments)
334+
{
335+
auto event = createEventImpl<Function>(wait);
336+
if (event) {
337+
addHelper(event, arguments...);
338+
postEventImpl(event);
339+
}
340+
return event;
341+
}
342+
343+
template<const GLFunction *Function>
344+
static QWebGLFunctionCall *createEventAndPostImpl(bool wait)
345+
{
346+
auto event = createEventImpl<Function>(wait);
347+
if (event)
348+
postEventImpl(event);
349+
return event;
327350
}
328351

329352
template<const GLFunction *Function, class... Ts>
330353
inline QWebGLFunctionCall *postEventImpl(bool wait, Ts&&... arguments)
331354
{
332-
return addHelper(createEvent<Function>(wait), arguments...);
355+
return createEventAndPostImpl<Function>(wait, arguments...);
333356
}
334357

335358
template<const GLFunction *Function>
336-
inline QWebGLFunctionCall *postEventImpl(bool wait)
359+
inline QWebGLFunctionCall *postEvent(bool wait)
337360
{
338-
return createEvent<Function>(wait);
361+
return createEventAndPostImpl<Function>(wait);
339362
}
340363

341364
template<const GLFunction *Function, class...Ts>

0 commit comments

Comments
 (0)