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

change receivers to share_ptr #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

change receivers to share_ptr #203

wants to merge 1 commit into from

Conversation

sicxs
Copy link

@sicxs sicxs commented Nov 27, 2017

change receivers to share_ptr

change receivers to share_ptr
@auscaster
Copy link
Member

Hi @sicxs just wondering the reason for using shared_ptr here? The Ref.alive boolean should always be set when the ref is dereferenced

@auscaster
Copy link
Member

I opted for the lightweight pointer approach here since the socket adapter is a single threaded event loop listener

@sicxs
Copy link
Author

sicxs commented Nov 28, 2017

When the server runs, everything is normal.
When the server is closed, if the removeReceiver is not called, it will lead to a memory leak.
But when the server is off, I also need to listen to the onSocketClose event, do the memory release, and not call the removeReceiver
This creates a contradiction. If shared_ptr is used, I don't need to call removeReceiver when the server is closed

@auscaster
Copy link
Member

auscaster commented Nov 28, 2017 via email

@sicxs
Copy link
Author

sicxs commented Nov 28, 2017

Please solve the problem of memory leakage
When the server is closed, make sure that there is no memory leak

@seawolf0820
Copy link

I fixed it in this way and no memory leak:

diff --git a/src/net/src/socketadapter.cpp b/src/net/src/socketadapter.cpp
index 2fdea5e2..1ad1ece0 100644
--- a/src/net/src/socketadapter.cpp
+++ b/src/net/src/socketadapter.cpp
@@ -32,6 +32,7 @@ SocketAdapter::SocketAdapter(SocketAdapter* sender)
 
 SocketAdapter::~SocketAdapter()
 {
+    for_each(_receivers.begin(), _receivers.end(), [=] (Ref* ref) { delete ref; });
     // LTrace("Destroy")
     // assert(_receivers.empty());
 }

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

Successfully merging this pull request may close these issues.

3 participants