diff --git a/src/file.cc b/src/file.cc index 60e503ad78..32298b8681 100644 --- a/src/file.cc +++ b/src/file.cc @@ -365,14 +365,14 @@ File::Write (const Arguments& args) // utf8 encoded data Local string = args[0]->ToString(); length = string->Utf8Length(); - buf = new char[length]; + buf = static_cast(malloc(length)); string->WriteUtf8(buf, length); } else if (args[0]->IsArray()) { // binary data Local array = Local::Cast(args[0]); length = array->Length(); - buf = new char[length]; + buf = static_cast(malloc(length)); for (int i = 0; i < length; i++) { Local int_value = array->Get(Integer::New(i)); buf[i] = int_value->Int32Value(); @@ -402,8 +402,8 @@ File::AfterWrite (eio_req *req) { File *file = static_cast(req->data); - char *buf = static_cast(req->ptr2); - delete buf; + //char *buf = static_cast(req->ptr2); + free(req->ptr2); size_t written = req->result; HandleScope scope; diff --git a/src/net.cc b/src/net.cc index 38db31a32c..6af7b0b4f4 100644 --- a/src/net.cc +++ b/src/net.cc @@ -18,6 +18,7 @@ using namespace v8; static Persistent socket_template; #define ON_CONNECT_SYMBOL String::NewSymbol("onConnect") +#define ON_CONNECTION_SYMBOL String::NewSymbol("onConnection") #define ON_READ_SYMBOL String::NewSymbol("onRead") static const struct addrinfo tcp_hints = @@ -101,6 +102,7 @@ Server::Server (Handle handle, int backlog) Server::~Server () { + HandleScope scope; oi_server_close(&server_); oi_server_detach(&server_); handle_.Dispose(); @@ -152,7 +154,7 @@ Server::ListenTCP (const Arguments& args) if (!args[callback_index]->IsFunction()) return ThrowException(String::New("Must supply onConnection callback")); - server->handle_->Set(String::NewSymbol("onConnection"), args[callback_index]); + server->handle_->Set(ON_CONNECTION_SYMBOL, args[callback_index]); r = oi_server_listen(&server->server_, address); if (r != 0) @@ -167,10 +169,9 @@ Server::ListenTCP (const Arguments& args) Handle Server::Close (const Arguments& args) { + HandleScope scope; Server *server = Server::Unwrap(args.Holder()); - oi_server_close(&server->server_); - return Undefined(); } @@ -180,10 +181,13 @@ Server::OnConnection (oi_server *s, struct sockaddr *remote_addr, socklen_t remo Server *server = static_cast (s->data); HandleScope scope; - Socket *socket = new Socket(socket_template->GetFunction()->NewInstance(), 60.0); + printf("DEBUG: got connection\n"); + + Local socket_handle = socket_template->GetFunction()->NewInstance(); + Socket *socket = new Socket(socket_handle, 60.0); socket->handle_->Delete(String::NewSymbol("connectTCP")); - Local callback_v = server->handle_->Get(String::NewSymbol("onConnection")); + Local callback_v = server->handle_->Get(ON_CONNECTION_SYMBOL); if (!callback_v->IsFunction()) return NULL; // produce error? @@ -307,6 +311,8 @@ Socket::ConnectTCP (const Arguments& args) */ node_eio_warmup(); eio_req *req = eio_custom (Socket::Resolve, EIO_PRI_DEFAULT, Socket::AfterResolve, socket); + + return Undefined(); } /* This function is executed in the thread pool. It cannot touch anything! */ @@ -387,8 +393,8 @@ Socket::Socket(Handle handle, double timeout) oi_socket_init(&socket_, timeout); socket_.on_connect = Socket::OnConnect; socket_.on_read = Socket::OnRead; - socket_.on_drain = Socket::OnDrain; - socket_.on_error = Socket::OnError; +// socket_.on_drain = Socket::OnDrain; +// socket_.on_error = Socket::OnError; socket_.on_close = Socket::OnClose; socket_.on_timeout = Socket::OnTimeout; socket_.data = this; @@ -405,10 +411,16 @@ Socket::Socket(Handle handle, double timeout) Socket::~Socket () { + HandleScope scope; oi_socket_close(&socket_); oi_socket_detach(&socket_); free(host_); free(port_); + + handle_->SetInternalField(0, Undefined()); + handle_->Delete(String::NewSymbol("write")); + handle_->Delete(String::NewSymbol("close")); + handle_.Dispose(); handle_.Clear(); // necessary? } @@ -416,6 +428,7 @@ Socket::~Socket () Handle Socket::SetEncoding (const Arguments& args) { + HandleScope scope; Socket *socket = Socket::Unwrap(args.Holder()); socket->SetEncoding(args[0]); return Undefined(); @@ -526,7 +539,10 @@ Socket::OnClose (oi_socket *s) HandleScope scope; Handle onclose_value = socket->handle_->Get( String::NewSymbol("onClose") ); - if (!onclose_value->IsFunction()) return; + if (!onclose_value->IsFunction()) { + delete socket; + return; + } Handle onclose = Handle::Cast(onclose_value); TryCatch try_catch; @@ -535,6 +551,8 @@ Socket::OnClose (oi_socket *s) if(try_catch.HasCaught()) node_fatal_exception(try_catch); + + delete socket; } void diff --git a/src/node.cc b/src/node.cc index 7a1c1148f9..bfb072b4d0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -115,7 +115,12 @@ NODE_METHOD(debug) static void OnFatalError (const char* location, const char* message) { - fprintf(stderr, "Fatal error: %s %s\n", location, message); +#define FATAL_ERROR "\033[1;31mV8 FATAL ERROR.\033[m" + if (location) + fprintf(stderr, FATAL_ERROR " %s %s\n", location, message); + else + fprintf(stderr, FATAL_ERROR " %s\n", message); + exit(1); }