Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2370)

Unified Diff: runtime/bin/socket.cc

Issue 2228503007: Fixes memory leaks in the eventhandler (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/platform/hashmap.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket.cc
diff --git a/runtime/bin/socket.cc b/runtime/bin/socket.cc
index 745306d0e53aef80dffdde95f79f249bda1bfe4d..f7bc5822d3c3924cfd4463cc7c3e3a495da64a2f 100644
--- a/runtime/bin/socket.cc
+++ b/runtime/bin/socket.cc
@@ -127,42 +127,57 @@ Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object,
}
-bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
+bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) {
ASSERT(!mutex_->TryLock());
+ ASSERT(os_socket != NULL);
+ ASSERT(os_socket->ref_count > 0);
+ os_socket->ref_count--;
+ if (os_socket->ref_count > 0) {
+ return false;
+ }
+ // We free the OS socket by removing it from two datastructures.
+ sockets_by_fd_.erase(os_socket->socketfd);
- SocketsIterator it = sockets_by_fd_.find(socketfd);
- if (it != sockets_by_fd_.end()) {
- OSSocket *os_socket = it->second;
+ OSSocket *prev = NULL;
+ OSSocket *current = sockets_by_port_[os_socket->port];
+ while (current != os_socket) {
+ ASSERT(current != NULL);
+ prev = current;
+ current = current->next;
+ }
- ASSERT(os_socket->ref_count > 0);
- os_socket->ref_count--;
- if (os_socket->ref_count == 0) {
- // We free the OS socket by removing it from two datastructures.
- sockets_by_fd_.erase(socketfd);
-
- OSSocket *prev = NULL;
- OSSocket *current = sockets_by_port_[os_socket->port];
- while (current != os_socket) {
- ASSERT(current != NULL);
- prev = current;
- current = current->next;
- }
+ if ((prev == NULL) && (current->next == NULL)) {
+ // Remove last element from the list.
+ sockets_by_port_.erase(os_socket->port);
+ } else if (prev == NULL) {
+ // Remove first element of the list.
+ sockets_by_port_[os_socket->port] = current->next;
+ } else {
+ // Remove element from the list which is not the first one.
+ prev->next = os_socket->next;
+ }
- if ((prev == NULL) && (current->next == NULL)) {
- // Remove last element from the list.
- sockets_by_port_.erase(os_socket->port);
- } else if (prev == NULL) {
- // Remove first element of the list.
- sockets_by_port_[os_socket->port] = current->next;
- } else {
- // Remove element from the list which is not the first one.
- prev->next = os_socket->next;
- }
+ ASSERT(os_socket->ref_count == 0);
+ delete os_socket;
+ return true;
+}
- delete os_socket;
- return true;
- }
- return false;
+
+void ListeningSocketRegistry::CloseAllSafe() {
+ MutexLocker ml(mutex_);
+ SocketsIterator it = sockets_by_fd_.begin();
+ while (it != sockets_by_fd_.end()) {
+ CloseOneSafe(it->second);
+ it++;
+ }
+}
+
+
+bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
+ ASSERT(!mutex_->TryLock());
+ SocketsIterator it = sockets_by_fd_.find(socketfd);
+ if (it != sockets_by_fd_.end()) {
+ return CloseOneSafe(it->second);
} else {
// It should be impossible for the event handler to close something that
// hasn't been created before.
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/platform/hashmap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698