Skip to content

Commit

Permalink
mem leak when releasing udp conn fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Jun 25, 2016
1 parent 816ee32 commit a872d8b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 1 addition & 2 deletions examples/udp-svr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ int main(int argc, const char* argv[]) {
exitif(!svr, "start udp server failed");
svr->onMsg([](const UdpServerPtr& p, Buffer buf, Ip4Addr peer) {
info("echo msg: %s to %s", buf.data(), peer.toString().c_str());
p->sendTo("", 0, peer);
//p->sendTo(buf, peer);
p->sendTo(buf, peer);
});
base.loop();
}
9 changes: 9 additions & 0 deletions handy/udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ UdpConnPtr UdpConn::createConnection(EventBase* base, const string& host, short
con->destHost_ = host;
con->destPort_ = port;
con->peer_ = addr;
con->base_ = base;
Channel* ch = new Channel(base, fd, kReadEvent);
con->channel_ = ch;
ch->onRead([con]{
Expand All @@ -110,6 +111,14 @@ UdpConnPtr UdpConn::createConnection(EventBase* base, const string& host, short
return con;
}

void UdpConn::close() {
if(!channel_)
return;
auto p = channel_;
channel_=NULL;
base_->safeCall([p](){ delete p; });
}

void UdpConn::send(const char *buf, size_t len) {
if (!channel_ || channel_->fd() < 0) {
warn("udp sending %lu bytes to %s after channel closed", len, peer_.toString().data());
Expand Down
2 changes: 1 addition & 1 deletion handy/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace handy {
void send(const std::string& s) { send(s.data(), s.size()); }
void send(const char* s) { send(s, strlen(s)); }
void onMsg(const UdpCallBack& cb) { cb_ = cb; }
void close() { if(!channel_) return; auto p = channel_; channel_=NULL; base_->safeCall([p](){ delete p; }); }
void close();
//远程地址的字符串
std::string str() { return peer_.toString(); }
public:
Expand Down

0 comments on commit a872d8b

Please sign in to comment.