Browse Source

Bugfix: EIOPromise::Create was allocating two EIOPromise objects

This is because it would call the javascript initializer which executed
Promise::New, and then it would rewrap the handle. Instead I make an
explicit inheritance from EIOPromise to Promise.

This seems to fix a memory leak which was reported by Ray Morgan:
http://groups.google.com/group/nodejs/browse_thread/thread/e38949b1989da1d7
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
76f4962636
  1. 6
      src/events.cc
  2. 1
      src/events.h
  3. 32
      src/file.cc
  4. 10
      src/file.h

6
src/events.cc

@ -221,11 +221,7 @@ Promise* Promise::Create(void) {
Local<Object> handle =
Promise::constructor_template->GetFunction()->NewInstance();
Promise *promise = new Promise();
promise->Wrap(handle);
promise->Attach();
return promise;
return ObjectWrap::Unwrap<Promise>(handle);
}
} // namespace node

1
src/events.h

@ -23,6 +23,7 @@ class EventEmitter : public ObjectWrap {
class Promise : public EventEmitter {
public:
static void Initialize(v8::Handle<v8::Object> target);
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static Promise* Create(void);

32
src/file.cc

@ -42,20 +42,24 @@ EIOPromise::Detach (void)
ev_unref(EV_DEFAULT_UC);
}
EIOPromise*
EIOPromise::Create (void)
{
Handle<Value> EIOPromise::New (const v8::Arguments& args) {
HandleScope scope;
Local<Object> handle =
Promise::constructor_template->GetFunction()->NewInstance();
EIOPromise *promise = new EIOPromise();
promise->Wrap(handle);
promise->Wrap(args.This());
promise->Attach();
return promise;
return args.This();
}
EIOPromise* EIOPromise::Create() {
HandleScope scope;
Local<Object> handle =
EIOPromise::constructor_template->GetFunction()->NewInstance();
return ObjectWrap::Unwrap<EIOPromise>(handle);
}
static Persistent<FunctionTemplate> stats_constructor_template;
@ -377,6 +381,8 @@ Read (const Arguments& args)
return scope.Close(EIOPromise::Read(fd, len, pos, encoding));
}
Persistent<FunctionTemplate> EIOPromise::constructor_template;
void
File::Initialize (Handle<Object> target)
{
@ -397,4 +403,14 @@ File::Initialize (Handle<Object> target)
stats_constructor_template = Persistent<FunctionTemplate>::New(t);
target->Set(String::NewSymbol("Stats"),
stats_constructor_template->GetFunction());
Local<FunctionTemplate> t2 = FunctionTemplate::New(EIOPromise::New);
EIOPromise::constructor_template = Persistent<FunctionTemplate>::New(t2);
EIOPromise::constructor_template->Inherit(
Promise::constructor_template);
EIOPromise::constructor_template->InstanceTemplate()->
SetInternalFieldCount(1);
target->Set(String::NewSymbol("EIOPromise"),
EIOPromise::constructor_template->GetFunction());
}

10
src/file.h

@ -27,6 +27,10 @@ public:
class EIOPromise : public Promise {
public:
static EIOPromise* Create();
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Object>
Open (const char *path, int flags, mode_t mode)
{
@ -111,12 +115,10 @@ class EIOPromise : public Promise {
return p->handle_;
}
static EIOPromise* Create (void);
protected:
void Attach (void);
void Detach (void);
void Attach ();
void Detach ();
EIOPromise () : Promise() { }

Loading…
Cancel
Save