You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just curious to know why WebSocketServer::OnConnectionCallback takes a weak_ptr as its first argument and not a shared_ptr?
I don't often use weak_ptr so I'm not sure what I should infer from it's use here. I am just coverting it to a shared_ptr and capturing in the socket's own OnMessageCallback with no apparent ill effects, is this OK? eg:
g_server->setOnConnectionCallback([](std::weak_ptr<ix::WebSocket> wsocket, std::shared_ptr<ix::ConnectionState>) { //
log() << "### WebSocketServer new connection!";
auto socket = wsocket.lock(); // convert to shared_ptr
socket->setOnMessageCallback([socket](const ix::WebSocketMessagePtr& wsMsg) { //
// blah blah blah...
socket->send(result);
});
And looking at the WebSocketServer code I can see it's originally a shared_ptr anyway so it seems a bit odd, although like I say I have no experience with weak_ptr (and not a lot with shared/unique ptrs either!)
Bye,
Mark
The text was updated successfully, but these errors were encountered:
iirc it was to avoid potentially creating cycles (which you can with shared_ptr), but honestly I don't remember now. I think the shared_ptr version was triggering some compiler or rather runtime warnings about leaked memory.
Hi,
Just curious to know why WebSocketServer::OnConnectionCallback takes a weak_ptr as its first argument and not a shared_ptr?
I don't often use weak_ptr so I'm not sure what I should infer from it's use here. I am just coverting it to a shared_ptr and capturing in the socket's own OnMessageCallback with no apparent ill effects, is this OK? eg:
And looking at the WebSocketServer code I can see it's originally a shared_ptr anyway so it seems a bit odd, although like I say I have no experience with weak_ptr (and not a lot with shared/unique ptrs either!)
Bye,
Mark
The text was updated successfully, but these errors were encountered: