Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about weak_ptr in API #477

Open
blitz-research opened this issue Jul 13, 2023 · 1 comment
Open

Question about weak_ptr in API #477

blitz-research opened this issue Jul 13, 2023 · 1 comment

Comments

@blitz-research
Copy link

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:

	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

@bsergean
Copy link
Collaborator

bsergean commented Aug 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants