Skip to content

io_service::stop leaks memory #490

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

Open
felixguendling opened this issue Dec 11, 2015 · 6 comments
Open

io_service::stop leaks memory #490

felixguendling opened this issue Dec 11, 2015 · 6 comments

Comments

@felixguendling
Copy link

I'm currently using the 0.3.0-alpha3 version in different projects. It's working fine!
Now I'm interested in supporting simple HTTP requests including request bodies. This functionality was added in 0.5.0.

In my projects I try to have a clean valgrind / Clang sanitizer statistic on shutdown. 0.3.0-alpha3 was clean in this sense. Unfortunately, the 0.6.0 version seems to "leak" memory. Probably it's not a repeated leak but it's polluting the valgrind statistic.

I tried to fix this but didn't find the exact reason for this leak. It seems that the initial connection that gets created when the server starts listening won't be freed. That's strange because it's memory-managed (shared pointer).

The following file contains the modified print_server.cpp (new: clean shutdown on SIGINT) and the valgrind output: report.txt

@felixguendling
Copy link
Author

Any ideas or hints where to look?

@zaphoyd zaphoyd added the Bug label Jan 31, 2016
@zaphoyd
Copy link
Owner

zaphoyd commented Jan 31, 2016

Short answer here is... use connection::close on existing connections and endpoint::stop_listening on the endpoint itself instead of endpoint::stop. Straight up stop forcibly stops a bunch of things in a way that bypasses most, if not all, of the cleanup routines.

At the moment, I would only recommend using endpoint::stop in conditions where you want to kill the entire program right now.

That said, supporting a io_service::stop operation while WebSocket++ is running on the io_service is something that should be done, so I'd like to leave this issue open to track it. My guess that the specific issue here is a circular dependency between a connection and itself that gets cleaned up by the standard connection closing procedures that don't get run when the WebSocket state machine is cut short. RAII/shared pointers cannot fix or prevent this issue directly.

@zaphoyd zaphoyd changed the title Leak? io_service::stop leaks memory Jan 31, 2016
@zaphoyd
Copy link
Owner

zaphoyd commented Jan 31, 2016

websocketpp::transport::asio::connection cached handlers (m_async_read_handler, lib::clear_function(m_async_write_handler, m_init_handler, m_read_handler, m_write_handler) are the likely culprits for the circular references.

xavigibert added a commit to xavigibert/websocketpp that referenced this issue Feb 18, 2016
…. This fix passes callbacks bound to weak_ptr's instead of shared_ptr's.
xavigibert added a commit to xavigibert/websocketpp that referenced this issue Feb 18, 2016
…. This fix passes callbacks bound to weak_ptr's instead of shared_ptr's.
@xavigibert
Copy link
Contributor

I submitted a pull request #525 which fixes this issue.

@felixguendling
Copy link
Author

Thank you very much! I will have a look at this soon and probably upgrade to your version.

zaphoyd pushed a commit that referenced this issue Feb 21, 2016
…t. references #490 #525

There isn't a clean way to implement this performance optimization without adding global state/locking, which performs worse. This should fix
@Rohitkorlam
Copy link

Hi, is this issue fixed and pushed to the develop/main branch?

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

No branches or pull requests

4 participants