Browse Source

fix some errors/memleaks

v0.7.4-release
Ryan 16 years ago
parent
commit
d996b59d9c
  1. 8
      src/file.cc
  2. 34
      src/net.cc
  3. 7
      src/node.cc

8
src/file.cc

@ -365,14 +365,14 @@ File::Write (const Arguments& args)
// utf8 encoded data // utf8 encoded data
Local<String> string = args[0]->ToString(); Local<String> string = args[0]->ToString();
length = string->Utf8Length(); length = string->Utf8Length();
buf = new char[length]; buf = static_cast<char*>(malloc(length));
string->WriteUtf8(buf, length); string->WriteUtf8(buf, length);
} else if (args[0]->IsArray()) { } else if (args[0]->IsArray()) {
// binary data // binary data
Local<Array> array = Local<Array>::Cast(args[0]); Local<Array> array = Local<Array>::Cast(args[0]);
length = array->Length(); length = array->Length();
buf = new char[length]; buf = static_cast<char*>(malloc(length));
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Local<Value> int_value = array->Get(Integer::New(i)); Local<Value> int_value = array->Get(Integer::New(i));
buf[i] = int_value->Int32Value(); buf[i] = int_value->Int32Value();
@ -402,8 +402,8 @@ File::AfterWrite (eio_req *req)
{ {
File *file = static_cast<File*>(req->data); File *file = static_cast<File*>(req->data);
char *buf = static_cast<char*>(req->ptr2); //char *buf = static_cast<char*>(req->ptr2);
delete buf; free(req->ptr2);
size_t written = req->result; size_t written = req->result;
HandleScope scope; HandleScope scope;

34
src/net.cc

@ -18,6 +18,7 @@ using namespace v8;
static Persistent<FunctionTemplate> socket_template; static Persistent<FunctionTemplate> socket_template;
#define ON_CONNECT_SYMBOL String::NewSymbol("onConnect") #define ON_CONNECT_SYMBOL String::NewSymbol("onConnect")
#define ON_CONNECTION_SYMBOL String::NewSymbol("onConnection")
#define ON_READ_SYMBOL String::NewSymbol("onRead") #define ON_READ_SYMBOL String::NewSymbol("onRead")
static const struct addrinfo tcp_hints = static const struct addrinfo tcp_hints =
@ -101,6 +102,7 @@ Server::Server (Handle<Object> handle, int backlog)
Server::~Server () Server::~Server ()
{ {
HandleScope scope;
oi_server_close(&server_); oi_server_close(&server_);
oi_server_detach(&server_); oi_server_detach(&server_);
handle_.Dispose(); handle_.Dispose();
@ -152,7 +154,7 @@ Server::ListenTCP (const Arguments& args)
if (!args[callback_index]->IsFunction()) if (!args[callback_index]->IsFunction())
return ThrowException(String::New("Must supply onConnection callback")); 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); r = oi_server_listen(&server->server_, address);
if (r != 0) if (r != 0)
@ -167,10 +169,9 @@ Server::ListenTCP (const Arguments& args)
Handle<Value> Handle<Value>
Server::Close (const Arguments& args) Server::Close (const Arguments& args)
{ {
HandleScope scope;
Server *server = Server::Unwrap(args.Holder()); Server *server = Server::Unwrap(args.Holder());
oi_server_close(&server->server_); oi_server_close(&server->server_);
return Undefined(); return Undefined();
} }
@ -180,10 +181,13 @@ Server::OnConnection (oi_server *s, struct sockaddr *remote_addr, socklen_t remo
Server *server = static_cast<Server*> (s->data); Server *server = static_cast<Server*> (s->data);
HandleScope scope; HandleScope scope;
Socket *socket = new Socket(socket_template->GetFunction()->NewInstance(), 60.0); printf("DEBUG: got connection\n");
Local<Object> socket_handle = socket_template->GetFunction()->NewInstance();
Socket *socket = new Socket(socket_handle, 60.0);
socket->handle_->Delete(String::NewSymbol("connectTCP")); socket->handle_->Delete(String::NewSymbol("connectTCP"));
Local<Value> callback_v = server->handle_->Get(String::NewSymbol("onConnection")); Local<Value> callback_v = server->handle_->Get(ON_CONNECTION_SYMBOL);
if (!callback_v->IsFunction()) if (!callback_v->IsFunction())
return NULL; // produce error? return NULL; // produce error?
@ -307,6 +311,8 @@ Socket::ConnectTCP (const Arguments& args)
*/ */
node_eio_warmup(); node_eio_warmup();
eio_req *req = eio_custom (Socket::Resolve, EIO_PRI_DEFAULT, Socket::AfterResolve, socket); 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! */ /* This function is executed in the thread pool. It cannot touch anything! */
@ -387,8 +393,8 @@ Socket::Socket(Handle<Object> handle, double timeout)
oi_socket_init(&socket_, timeout); oi_socket_init(&socket_, timeout);
socket_.on_connect = Socket::OnConnect; socket_.on_connect = Socket::OnConnect;
socket_.on_read = Socket::OnRead; socket_.on_read = Socket::OnRead;
socket_.on_drain = Socket::OnDrain; // socket_.on_drain = Socket::OnDrain;
socket_.on_error = Socket::OnError; // socket_.on_error = Socket::OnError;
socket_.on_close = Socket::OnClose; socket_.on_close = Socket::OnClose;
socket_.on_timeout = Socket::OnTimeout; socket_.on_timeout = Socket::OnTimeout;
socket_.data = this; socket_.data = this;
@ -405,10 +411,16 @@ Socket::Socket(Handle<Object> handle, double timeout)
Socket::~Socket () Socket::~Socket ()
{ {
HandleScope scope;
oi_socket_close(&socket_); oi_socket_close(&socket_);
oi_socket_detach(&socket_); oi_socket_detach(&socket_);
free(host_); free(host_);
free(port_); free(port_);
handle_->SetInternalField(0, Undefined());
handle_->Delete(String::NewSymbol("write"));
handle_->Delete(String::NewSymbol("close"));
handle_.Dispose(); handle_.Dispose();
handle_.Clear(); // necessary? handle_.Clear(); // necessary?
} }
@ -416,6 +428,7 @@ Socket::~Socket ()
Handle<Value> Handle<Value>
Socket::SetEncoding (const Arguments& args) Socket::SetEncoding (const Arguments& args)
{ {
HandleScope scope;
Socket *socket = Socket::Unwrap(args.Holder()); Socket *socket = Socket::Unwrap(args.Holder());
socket->SetEncoding(args[0]); socket->SetEncoding(args[0]);
return Undefined(); return Undefined();
@ -526,7 +539,10 @@ Socket::OnClose (oi_socket *s)
HandleScope scope; HandleScope scope;
Handle<Value> onclose_value = socket->handle_->Get( String::NewSymbol("onClose") ); Handle<Value> onclose_value = socket->handle_->Get( String::NewSymbol("onClose") );
if (!onclose_value->IsFunction()) return; if (!onclose_value->IsFunction()) {
delete socket;
return;
}
Handle<Function> onclose = Handle<Function>::Cast(onclose_value); Handle<Function> onclose = Handle<Function>::Cast(onclose_value);
TryCatch try_catch; TryCatch try_catch;
@ -535,6 +551,8 @@ Socket::OnClose (oi_socket *s)
if(try_catch.HasCaught()) if(try_catch.HasCaught())
node_fatal_exception(try_catch); node_fatal_exception(try_catch);
delete socket;
} }
void void

7
src/node.cc

@ -115,7 +115,12 @@ NODE_METHOD(debug)
static void static void
OnFatalError (const char* location, const char* message) 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); exit(1);
} }

Loading…
Cancel
Save