• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <node.h>
2 #include <v8.h>
3 #include <uv.h>
4 
5 using v8::Context;
6 using v8::FunctionCallbackInfo;
7 using v8::Isolate;
8 using v8::Local;
9 using v8::Object;
10 using v8::Value;
11 
12 // Give these things names in the public namespace so that we can see
13 // them show up in symbol dumps.
CloseCallback(uv_handle_t * handle)14 void CloseCallback(uv_handle_t* handle) {}
15 
16 class ExampleOwnerClass {
17  public:
18   virtual ~ExampleOwnerClass();
19 };
20 
21 // Do not inline this into the class, because that may remove the virtual
22 // table when LTO is used, and with it the symbol for which we grep the process
23 // output in test/abort/test-addon-uv-handle-leak.
24 // When the destructor is not inlined, the compiler will have to assume that it,
25 // and the vtable, is part of what this compilation unit exports, and keep them.
~ExampleOwnerClass()26 ExampleOwnerClass::~ExampleOwnerClass() {}
27 
28 ExampleOwnerClass example_instance;
29 
LeakHandle(const FunctionCallbackInfo<Value> & args)30 void LeakHandle(const FunctionCallbackInfo<Value>& args) {
31   Isolate* isolate = args.GetIsolate();
32   Local<Context> context = isolate->GetCurrentContext();
33   uv_loop_t* loop = node::GetCurrentEventLoop(isolate);
34   assert(loop != nullptr);
35 
36   uv_timer_t* leaked_timer = new uv_timer_t;
37   leaked_timer->close_cb = CloseCallback;
38 
39   if (args[0]->IsNumber()) {
40     leaked_timer->data =
41         reinterpret_cast<void*>(args[0]->IntegerValue(context).FromJust());
42   } else {
43     leaked_timer->data = &example_instance;
44   }
45 
46   uv_timer_init(loop, leaked_timer);
47   uv_timer_start(leaked_timer, [](uv_timer_t*){}, 1000, 1000);
48   uv_unref(reinterpret_cast<uv_handle_t*>(leaked_timer));
49 }
50 
51 // This module gets loaded multiple times in some tests so it must support that.
NODE_MODULE_INIT()52 NODE_MODULE_INIT(/*exports, module, context*/) {
53   NODE_SET_METHOD(exports, "leakHandle", LeakHandle);
54 }
55