Commits

Anonymous committed 76a0855

Delete uv_poll_t watcher in uv_close() OnClose callback instead of in the wrapped C++ objects deconstructor

Comments (0)

Files changed (3)

  * Using uint16_t instead of uint32_t on line 87 in src/raw.cc for a value
    that is out of range
  * raw::SocketWrap::pause() only uses the first argument
+ * Delete uv_poll_t watcher in uv_close() OnClose callback instead of in the
+   wrapped C++ objects deconstructor
 
 # Roadmap
 
 	target->Set (String::NewSymbol ("SocketWrap"), tpl->GetFunction ());
 }
 
-SocketWrap::SocketWrap () {}
+SocketWrap::SocketWrap () {
+	deconstructing_ = false;
+}
 
 SocketWrap::~SocketWrap () {
+	deconstructing_ = true;
 	this->CloseSocket ();
 }
 
 	HandleScope scope;
 	
 	if (this->poll_initialised_) {
-		uv_close ((uv_handle_t *) &this->poll_watcher_, OnClose);
+		uv_close ((uv_handle_t *) this->poll_watcher_, OnClose);
 		closesocket (this->poll_fd_);
 		this->poll_fd_ = INVALID_SOCKET;
 		this->poll_initialised_ = false;
 		return SOCKET_ERRNO;
 #endif
 
-	uv_poll_init_socket (uv_default_loop (), &this->poll_watcher_,
+	poll_watcher_ = new uv_poll_t;
+	uv_poll_init_socket (uv_default_loop (), this->poll_watcher_,
 			this->poll_fd_);
-	this->poll_watcher_.data = this;
-	uv_poll_start (&this->poll_watcher_, UV_READABLE, IoEvent);
+	this->poll_watcher_->data = this;
+	uv_poll_start (this->poll_watcher_, UV_READABLE, IoEvent);
 	
 	this->poll_initialised_ = true;
 	
 }
 
 void SocketWrap::OnClose (uv_handle_t *handle) {
-	// We can re-use the socket so we won't actually do anything here
+	delete handle;
 }
 
 Handle<Value> SocketWrap::Pause (const Arguments& args) {
 	int events = (pause_recv ? 0 : UV_READABLE)
 			| (pause_send ? 0 : UV_WRITABLE);
 
-	uv_poll_stop (&socket->poll_watcher_);
-	uv_poll_start (&socket->poll_watcher_, events, IoEvent);
+	if (! socket->deconstructing_) {
+		uv_poll_stop (socket->poll_watcher_);
+		if (events)
+			uv_poll_start (socket->poll_watcher_, events, IoEvent);
+	}
 	
 	return scope.Close (args.This ());
 }
 	uint32_t protocol_;
 
 	SOCKET poll_fd_;
-	uv_poll_t poll_watcher_;
+	uv_poll_t *poll_watcher_;
 	bool poll_initialised_;
+	
+	bool deconstructing_;
 };
 
 static void IoEvent (uv_poll_t* watcher, int status, int revents);