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