@@ -1796,22 +1796,25 @@ static void Query(const FunctionCallbackInfo<Value>& args) {
1796
1796
1797
1797
Local<Object> req_wrap_obj = args[0 ].As <Object>();
1798
1798
Local<String> string = args[1 ].As <String>();
1799
- Wrap* wrap = new Wrap (channel, req_wrap_obj);
1799
+ auto wrap = std::make_unique< Wrap> (channel, req_wrap_obj);
1800
1800
1801
1801
node::Utf8Value name (env->isolate (), string);
1802
1802
channel->ModifyActivityQueryCount (1 );
1803
1803
int err = wrap->Send (*name);
1804
1804
if (err) {
1805
1805
channel->ModifyActivityQueryCount (-1 );
1806
- delete wrap;
1806
+ } else {
1807
+ // Release ownership of the pointer allowing the ownership to be transferred
1808
+ USE (wrap.release ());
1807
1809
}
1808
1810
1809
1811
args.GetReturnValue ().Set (err);
1810
1812
}
1811
1813
1812
1814
1813
1815
void AfterGetAddrInfo (uv_getaddrinfo_t * req, int status, struct addrinfo * res) {
1814
- GetAddrInfoReqWrap* req_wrap = static_cast <GetAddrInfoReqWrap*>(req->data );
1816
+ std::unique_ptr<GetAddrInfoReqWrap> req_wrap {
1817
+ static_cast <GetAddrInfoReqWrap*>(req->data )};
1815
1818
Environment* env = req_wrap->env ();
1816
1819
1817
1820
HandleScope handle_scope (env->isolate ());
@@ -1868,21 +1871,20 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
1868
1871
uv_freeaddrinfo (res);
1869
1872
1870
1873
TRACE_EVENT_NESTABLE_ASYNC_END2 (
1871
- TRACING_CATEGORY_NODE2 (dns, native), " lookup" , req_wrap,
1874
+ TRACING_CATEGORY_NODE2 (dns, native), " lookup" , req_wrap. get () ,
1872
1875
" count" , n, " verbatim" , verbatim);
1873
1876
1874
1877
// Make the callback into JavaScript
1875
1878
req_wrap->MakeCallback (env->oncomplete_string (), arraysize (argv), argv);
1876
-
1877
- delete req_wrap;
1878
1879
}
1879
1880
1880
1881
1881
1882
void AfterGetNameInfo (uv_getnameinfo_t * req,
1882
1883
int status,
1883
1884
const char * hostname,
1884
1885
const char * service) {
1885
- GetNameInfoReqWrap* req_wrap = static_cast <GetNameInfoReqWrap*>(req->data );
1886
+ std::unique_ptr<GetNameInfoReqWrap> req_wrap {
1887
+ static_cast <GetNameInfoReqWrap*>(req->data )};
1886
1888
Environment* env = req_wrap->env ();
1887
1889
1888
1890
HandleScope handle_scope (env->isolate ());
@@ -1903,14 +1905,12 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
1903
1905
}
1904
1906
1905
1907
TRACE_EVENT_NESTABLE_ASYNC_END2 (
1906
- TRACING_CATEGORY_NODE2 (dns, native), " lookupService" , req_wrap,
1908
+ TRACING_CATEGORY_NODE2 (dns, native), " lookupService" , req_wrap. get () ,
1907
1909
" hostname" , TRACE_STR_COPY (hostname),
1908
1910
" service" , TRACE_STR_COPY (service));
1909
1911
1910
1912
// Make the callback into JavaScript
1911
1913
req_wrap->MakeCallback (env->oncomplete_string (), arraysize (argv), argv);
1912
-
1913
- delete req_wrap;
1914
1914
}
1915
1915
1916
1916
using ParseIPResult = decltype(static_cast <ares_addr_port_node*>(0 )->addr);
@@ -1970,7 +1970,9 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
1970
1970
CHECK (0 && " bad address family" );
1971
1971
}
1972
1972
1973
- auto req_wrap = new GetAddrInfoReqWrap (env, req_wrap_obj, args[4 ]->IsTrue ());
1973
+ auto req_wrap = std::make_unique<GetAddrInfoReqWrap>(env,
1974
+ req_wrap_obj,
1975
+ args[4 ]->IsTrue ());
1974
1976
1975
1977
struct addrinfo hints;
1976
1978
memset (&hints, 0 , sizeof (struct addrinfo ));
@@ -1979,7 +1981,7 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
1979
1981
hints.ai_flags = flags;
1980
1982
1981
1983
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2 (
1982
- TRACING_CATEGORY_NODE2 (dns, native), " lookup" , req_wrap,
1984
+ TRACING_CATEGORY_NODE2 (dns, native), " lookup" , req_wrap. get () ,
1983
1985
" hostname" , TRACE_STR_COPY (*hostname),
1984
1986
" family" ,
1985
1987
family == AF_INET ? " ipv4" : family == AF_INET6 ? " ipv6" : " unspec" );
@@ -1989,8 +1991,9 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
1989
1991
*hostname,
1990
1992
nullptr ,
1991
1993
&hints);
1992
- if (err)
1993
- delete req_wrap;
1994
+ if (err == 0 )
1995
+ // Release ownership of the pointer allowing the ownership to be transferred
1996
+ USE (req_wrap.release ());
1994
1997
1995
1998
args.GetReturnValue ().Set (err);
1996
1999
}
@@ -2010,18 +2013,19 @@ void GetNameInfo(const FunctionCallbackInfo<Value>& args) {
2010
2013
CHECK (uv_ip4_addr (*ip, port, reinterpret_cast <sockaddr_in*>(&addr)) == 0 ||
2011
2014
uv_ip6_addr (*ip, port, reinterpret_cast <sockaddr_in6*>(&addr)) == 0 );
2012
2015
2013
- GetNameInfoReqWrap* req_wrap = new GetNameInfoReqWrap (env, req_wrap_obj);
2016
+ auto req_wrap = std::make_unique< GetNameInfoReqWrap> (env, req_wrap_obj);
2014
2017
2015
2018
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2 (
2016
- TRACING_CATEGORY_NODE2 (dns, native), " lookupService" , req_wrap,
2019
+ TRACING_CATEGORY_NODE2 (dns, native), " lookupService" , req_wrap. get () ,
2017
2020
" ip" , TRACE_STR_COPY (*ip), " port" , port);
2018
2021
2019
2022
int err = req_wrap->Dispatch (uv_getnameinfo,
2020
2023
AfterGetNameInfo,
2021
2024
reinterpret_cast <struct sockaddr *>(&addr),
2022
2025
NI_NAMEREQD);
2023
- if (err)
2024
- delete req_wrap;
2026
+ if (err == 0 )
2027
+ // Release ownership of the pointer allowing the ownership to be transferred
2028
+ USE (req_wrap.release ());
2025
2029
2026
2030
args.GetReturnValue ().Set (err);
2027
2031
}
0 commit comments