@@ -258,8 +258,12 @@ NAN_METHOD(CreateNode) {
258258 name_space.c_str (), context, &options),
259259 rcl_get_error_string ().str );
260260
261- auto handle = RclHandle::NewInstance (node, nullptr ,
262- [node] { return rcl_node_fini (node); });
261+ auto handle = RclHandle::NewInstance (node, nullptr , [](void * ptr) {
262+ rcl_node_t * node = reinterpret_cast <rcl_node_t *>(ptr);
263+ rcl_ret_t ret = rcl_node_fini (node);
264+ free (ptr);
265+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
266+ });
263267 info.GetReturnValue ().Set (handle);
264268}
265269
@@ -280,8 +284,12 @@ NAN_METHOD(CreateGuardCondition) {
280284 rcl_guard_condition_init (gc, context, gc_options),
281285 rcl_get_error_string ().str );
282286
283- auto handle = RclHandle::NewInstance (
284- gc, nullptr , [gc] { return rcl_guard_condition_fini (gc); });
287+ auto handle = RclHandle::NewInstance (gc, nullptr , [](void * ptr) {
288+ rcl_guard_condition_t * gc = reinterpret_cast <rcl_guard_condition_t *>(ptr);
289+ rcl_ret_t ret = rcl_guard_condition_fini (gc);
290+ free (ptr);
291+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
292+ });
285293 info.GetReturnValue ().Set (handle);
286294}
287295
@@ -315,8 +323,12 @@ NAN_METHOD(CreateTimer) {
315323 rcl_get_default_allocator ()),
316324 rcl_get_error_string ().str );
317325
318- auto js_obj = RclHandle::NewInstance (
319- timer, clock_handle, [timer] { return rcl_timer_fini (timer); });
326+ auto js_obj = RclHandle::NewInstance (timer, clock_handle, [](void * ptr) {
327+ rcl_timer_t * timer = reinterpret_cast <rcl_timer_t *>(ptr);
328+ rcl_ret_t ret = rcl_timer_fini (timer);
329+ free (ptr);
330+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
331+ });
320332 info.GetReturnValue ().Set (js_obj);
321333}
322334
@@ -411,7 +423,8 @@ NAN_METHOD(CreateTimePoint) {
411423 time_point->nanoseconds = std::stoll (str);
412424 time_point->clock_type = static_cast <rcl_clock_type_t >(clock_type);
413425
414- auto js_obj = RclHandle::NewInstance (time_point, nullptr , nullptr );
426+ auto js_obj =
427+ RclHandle::NewInstance (time_point, nullptr , [](void * ptr) { free (ptr); });
415428 info.GetReturnValue ().Set (js_obj);
416429}
417430
@@ -431,7 +444,8 @@ NAN_METHOD(CreateDuration) {
431444 reinterpret_cast <rcl_duration_t *>(malloc (sizeof (rcl_duration_t )));
432445 duration->nanoseconds = std::stoll (str);
433446
434- auto js_obj = RclHandle::NewInstance (duration, nullptr , nullptr );
447+ auto js_obj =
448+ RclHandle::NewInstance (duration, nullptr , [](void * ptr) { free (ptr); });
435449 info.GetReturnValue ().Set (js_obj);
436450}
437451
@@ -500,8 +514,13 @@ NAN_METHOD(CreateClock) {
500514 rcl_clock_init (clock_type, clock, &allocator),
501515 rcl_get_error_string ().str );
502516
503- info.GetReturnValue ().Set (RclHandle::NewInstance (
504- clock, nullptr , [clock]() { return rcl_clock_fini (clock); }));
517+ info.GetReturnValue ().Set (
518+ RclHandle::NewInstance (clock, nullptr , [](void * ptr) {
519+ rcl_clock_t * clock = reinterpret_cast <rcl_clock_t *>(ptr);
520+ rcl_ret_t ret = rcl_clock_fini (clock);
521+ free (ptr);
522+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
523+ }));
505524}
506525
507526static void ReturnJSTimeObj (
@@ -654,8 +673,12 @@ NAN_METHOD(CreateSubscription) {
654673 rcl_get_error_string ().str );
655674
656675 auto js_obj =
657- RclHandle::NewInstance (subscription, node_handle, [subscription, node] {
658- return rcl_subscription_fini (subscription, node);
676+ RclHandle::NewInstance (subscription, node_handle, [node](void * ptr) {
677+ rcl_subscription_t * subscription =
678+ reinterpret_cast <rcl_subscription_t *>(ptr);
679+ rcl_ret_t ret = rcl_subscription_fini (subscription, node);
680+ free (ptr);
681+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
659682 });
660683 info.GetReturnValue ().Set (js_obj);
661684 } else {
@@ -702,9 +725,13 @@ NAN_METHOD(CreatePublisher) {
702725 RCL_RET_OK, rcl_get_error_string ().str );
703726
704727 // Wrap the handle into JS object
705- auto js_obj = RclHandle::NewInstance (
706- publisher, node_handle,
707- [publisher, node]() { return rcl_publisher_fini (publisher, node); });
728+ auto js_obj =
729+ RclHandle::NewInstance (publisher, node_handle, [node](void * ptr) {
730+ rcl_publisher_t * publisher = reinterpret_cast <rcl_publisher_t *>(ptr);
731+ rcl_ret_t ret = rcl_publisher_fini (publisher, node);
732+ free (ptr);
733+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
734+ });
708735
709736 // Everything is done
710737 info.GetReturnValue ().Set (js_obj);
@@ -767,9 +794,13 @@ NAN_METHOD(CreateClient) {
767794 rcl_client_init (client, node, ts, service_name.c_str (), &client_ops),
768795 RCL_RET_OK, rcl_get_error_string ().str );
769796
770- auto js_obj = RclHandle::NewInstance (client, node_handle, [client, node] {
771- return rcl_client_fini (client, node);
772- });
797+ auto js_obj =
798+ RclHandle::NewInstance (client, node_handle, [node](void * ptr) {
799+ rcl_client_t * client = reinterpret_cast <rcl_client_t *>(ptr);
800+ rcl_ret_t ret = rcl_client_fini (client, node);
801+ free (ptr);
802+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
803+ });
773804
774805 info.GetReturnValue ().Set (js_obj);
775806 } else {
@@ -846,9 +877,13 @@ NAN_METHOD(CreateService) {
846877 THROW_ERROR_IF_NOT_EQUAL (
847878 rcl_service_init (service, node, ts, service_name.c_str (), &service_ops),
848879 RCL_RET_OK, rcl_get_error_string ().str );
849- auto js_obj = RclHandle::NewInstance (service, node_handle, [service, node] {
850- return rcl_service_fini (service, node);
851- });
880+ auto js_obj =
881+ RclHandle::NewInstance (service, node_handle, [node](void * ptr) {
882+ rcl_service_t * service = reinterpret_cast <rcl_service_t *>(ptr);
883+ rcl_ret_t ret = rcl_service_fini (service, node);
884+ free (ptr);
885+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
886+ });
852887
853888 info.GetReturnValue ().Set (js_obj);
854889 } else {
@@ -868,7 +903,8 @@ NAN_METHOD(RclTakeRequest) {
868903 node::Buffer::Data (Nan::To<v8::Object>(info[2 ]).ToLocalChecked ());
869904 rcl_ret_t ret = rcl_take_request (service, header, taken_request);
870905 if (ret != RCL_RET_SERVICE_TAKE_FAILED) {
871- auto js_obj = RclHandle::NewInstance (header);
906+ auto js_obj =
907+ RclHandle::NewInstance (header, nullptr , [](void * ptr) { free (ptr); });
872908 info.GetReturnValue ().Set (js_obj);
873909 return ;
874910 }
@@ -1294,7 +1330,7 @@ NAN_METHOD(CreateArrayBufferCleaner) {
12941330
12951331 char * target = *reinterpret_cast <char **>(address + offset);
12961332 info.GetReturnValue ().Set (
1297- RclHandle::NewInstance (target, nullptr , [] { return RCL_RET_OK ; }));
1333+ RclHandle::NewInstance (target, nullptr , []( void * ptr) { free (ptr) ; }));
12981334}
12991335
13001336NAN_METHOD (setLoggerLevel) {
@@ -1378,8 +1414,12 @@ NAN_METHOD(CreateContext) {
13781414 rcl_context_t * context =
13791415 reinterpret_cast <rcl_context_t *>(malloc (sizeof (rcl_context_t )));
13801416 *context = rcl_get_zero_initialized_context ();
1381- auto js_obj = RclHandle::NewInstance (context, nullptr ,
1382- std::bind (DestroyContext, context));
1417+ auto js_obj = RclHandle::NewInstance (context, nullptr , [](void * ptr) {
1418+ rcl_context_t * context = reinterpret_cast <rcl_context_t *>(ptr);
1419+ rcl_ret_t ret = DestroyContext (context);
1420+ free (ptr);
1421+ THROW_ERROR_IF_NOT_EQUAL (RCL_RET_OK, ret, rcl_get_error_string ().str );
1422+ });
13831423
13841424 info.GetReturnValue ().Set (js_obj);
13851425}
0 commit comments