Skip to content

Commit 3e075fe

Browse files
committed
Merge branch 'ws_fix' of https://git01.codeplex.com/forks/jicailiu/xsapi into development
2 parents 9651b67 + bbd3e36 commit 3e075fe

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Release/src/websockets/client/ws_client_winrt.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,26 @@ class winrt_callback_client : public websocket_client_callback_impl, public std:
179179
m_msg_websocket->MessageReceived += ref new TypedEventHandler<MessageWebSocket^, MessageWebSocketMessageReceivedEventArgs^>(m_context, &ReceiveContext::OnReceive);
180180
m_msg_websocket->Closed += ref new TypedEventHandler<IWebSocket^, WebSocketClosedEventArgs^>(m_context, &ReceiveContext::OnClosed);
181181

182-
return pplx::create_task(m_msg_websocket->ConnectAsync(uri)).then([=](pplx::task<void> result) -> pplx::task<void>
182+
std::weak_ptr<winrt_callback_client> thisWeakPtr = shared_from_this();
183+
return pplx::create_task(m_msg_websocket->ConnectAsync(uri)).then([thisWeakPtr](pplx::task<void> result) -> pplx::task<void>
183184
{
184185
try
185186
{
187+
// result.get() should happen before anything else, to make sure there is no unobserved exception
188+
// in the task chain.
186189
result.get();
187-
m_messageWriter = ref new DataWriter(m_msg_websocket->OutputStream);
190+
191+
std::shared_ptr<winrt_callback_client> pThis(thisWeakPtr.lock());
192+
if (pThis == nullptr)
193+
{
194+
throw websocket_exception("Websocket client is being destroyed");
195+
}
196+
197+
pThis->m_messageWriter = ref new DataWriter(m_msg_websocket->OutputStream);
188198
}
189199
catch (Platform::Exception^ e)
190200
{
191-
websocket_exception exc(e->HResult, build_error_msg(e, "ConnectAsync"));
192-
return pplx::task_from_exception<void>(exc);
201+
throw websocket_exception(e->HResult, build_error_msg(e, "ConnectAsync"));
193202
}
194203
m_connected = true;
195204
return pplx::task_from_result();

0 commit comments

Comments
 (0)