-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement ObjectWrap from scratch to prevent memory leaks.
- Loading branch information
Showing
3 changed files
with
107 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/********************************************************************* | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2015 NAN contributors | ||
* | ||
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> | ||
********************************************************************/ | ||
|
||
#ifndef NAN_OBJECT_WRAP_H_ | ||
#define NAN_OBJECT_WRAP_H_ | ||
|
||
class ObjectWrap { | ||
public: | ||
ObjectWrap() { | ||
refs_ = 0; | ||
} | ||
|
||
|
||
virtual ~ObjectWrap() { | ||
} | ||
|
||
|
||
template <class T> | ||
static inline T* Unwrap(v8::Local<v8::Object> handle) { | ||
assert(!handle.IsEmpty()); | ||
assert(handle->InternalFieldCount() > 0); | ||
// Cast to ObjectWrap before casting to T. A direct cast from void | ||
// to T won't work right when T has more than one base class. | ||
void* ptr = GetInternalFieldPointer(handle, 0); | ||
ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr); | ||
return static_cast<T*>(wrap); | ||
} | ||
|
||
|
||
inline v8::Local<v8::Object> handle() { | ||
return New(persistent()); | ||
} | ||
|
||
|
||
inline Persistent<v8::Object>& persistent() { | ||
return handle_; | ||
} | ||
|
||
|
||
protected: | ||
inline void Wrap(v8::Local<v8::Object> handle) { | ||
assert(persistent().IsEmpty()); | ||
assert(handle->InternalFieldCount() > 0); | ||
SetInternalFieldPointer(handle, 0, this); | ||
persistent().Reset(handle); | ||
MakeWeak(); | ||
} | ||
|
||
|
||
inline void MakeWeak() { | ||
persistent().SetWeak(this, WeakCallback, WeakCallbackType::kParameter); | ||
persistent().MarkIndependent(); | ||
} | ||
|
||
/* Ref() marks the object as being attached to an event loop. | ||
* Refed objects will not be garbage collected, even if | ||
* all references are lost. | ||
*/ | ||
virtual void Ref() { | ||
assert(!persistent().IsEmpty()); | ||
persistent().ClearWeak(); | ||
refs_++; | ||
} | ||
|
||
/* Unref() marks an object as detached from the event loop. This is its | ||
* default state. When an object with a "weak" reference changes from | ||
* attached to detached state it will be freed. Be careful not to access | ||
* the object after making this call as it might be gone! | ||
* (A "weak reference" means an object that only has a | ||
* persistant handle.) | ||
* | ||
* DO NOT CALL THIS FROM DESTRUCTOR | ||
*/ | ||
virtual void Unref() { | ||
assert(!persistent().IsEmpty()); | ||
assert(!persistent().IsWeak()); | ||
assert(refs_ > 0); | ||
if (--refs_ == 0) | ||
MakeWeak(); | ||
} | ||
|
||
int refs_; // ro | ||
|
||
private: | ||
static void WeakCallback( | ||
const WeakCallbackInfo<ObjectWrap>& data) { | ||
HandleScope scope; | ||
ObjectWrap* wrap = data.GetParameter(); | ||
assert(wrap->refs_ == 0); | ||
delete wrap; | ||
} | ||
|
||
Persistent<v8::Object> handle_; | ||
}; | ||
|
||
|
||
#endif // NAN_OBJECT_WRAP_H_ |