Skip to content

Commit

Permalink
udp: use MaybeStackBuffer on DoSend
Browse files Browse the repository at this point in the history
instead of creating own buffer, use MaybeStackBuffer on DoSend to take advantage of its destructor to free up memory, so buffer never leaks memory - even if code in DoSend throws.
  • Loading branch information
Paul Kiddie committed Sep 17, 2016
1 parent a89c23f commit a345322
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/udp_wrap.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -275,13 +275,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback); SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
size_t msg_size = 0; size_t msg_size = 0;


// allocate uv_buf_t of the correct size MaybeStackBuffer<uv_buf_t> bufs(count);
// if bigger than 16 elements
uv_buf_t bufs_[16];
uv_buf_t* bufs = bufs_;

if (arraysize(bufs_) < count)
bufs = new uv_buf_t[count];


// construct uv_buf_t array // construct uv_buf_t array
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
Expand Down Expand Up @@ -313,16 +307,12 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
if (err == 0) { if (err == 0) {
err = uv_udp_send(&req_wrap->req_, err = uv_udp_send(&req_wrap->req_,
&wrap->handle_, &wrap->handle_,
bufs, *bufs,
count, count,
reinterpret_cast<const sockaddr*>(&addr), reinterpret_cast<const sockaddr*>(&addr),
OnSend); OnSend);
} }


// Deallocate space
if (bufs != bufs_)
delete[] bufs;

req_wrap->Dispatched(); req_wrap->Dispatched();
if (err) if (err)
delete req_wrap; delete req_wrap;
Expand Down

0 comments on commit a345322

Please sign in to comment.