Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: SrsMetaCache memleak; getaddrinfo use delete. #2880

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion trunk/doc/source.flv

This file was deleted.

Binary file added trunk/doc/source.flv
Binary file not shown.
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,8 @@ SrsMetaCache::SrsMetaCache()
SrsMetaCache::~SrsMetaCache()
{
dispose();
srs_freep(vformat);
srs_freep(aformat);
}

void SrsMetaCache::dispose()
Expand Down
10 changes: 5 additions & 5 deletions trunk/src/kernel/srs_kernel_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ string srs_dns_resolve(string host, int& family)
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;

addrinfo* r = NULL;
SrsAutoFree(addrinfo, r);
SrsAutoFreeH(addrinfo, r, freeaddrinfo);
if(getaddrinfo(host.c_str(), NULL, &hints, &r)) {
return "";
}

char shost[64];
memset(shost, 0, sizeof(shost));
if (getnameinfo(r->ai_addr, r->ai_addrlen, shost, sizeof(shost), NULL, 0, NI_NUMERICHOST)) {
return "";
}

family = r->ai_family;
return string(shost);
family = r->ai_family;
return string(shost);
}

void srs_parse_hostport(string hostport, string& host, int& port)
Expand Down
23 changes: 12 additions & 11 deletions trunk/src/protocol/srs_service_st.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,41 +158,41 @@ srs_error_t srs_tcp_connect(string server, int port, srs_utime_t tm, srs_netfd_t
if (tm != SRS_UTIME_NO_TIMEOUT) {
timeout = tm;
}

*pstfd = NULL;
srs_netfd_t stfd = NULL;

char sport[8];
snprintf(sport, sizeof(sport), "%d", port);

addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

addrinfo* r = NULL;
SrsAutoFree(addrinfo, r);
Copy link
Member

@winlinvip winlinvip Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use SrsAutoFree?

If you need to use free instead of delete for deallocation, you can use SrsAutoFreeF.

Automatic freeing can significantly improve code readability.

TRANS_BY_GPT3

SrsAutoFreeH(addrinfo, r, freeaddrinfo);
if(getaddrinfo(server.c_str(), sport, (const addrinfo*)&hints, &r)) {
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "get address info");
}

int sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
if(sock == -1){
return srs_error_new(ERROR_SOCKET_CREATE, "create socket");
}

srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
::close(sock);
return srs_error_new(ERROR_ST_OPEN_SOCKET, "open socket");
}

if (st_connect((st_netfd_t)stfd, r->ai_addr, r->ai_addrlen, timeout) == -1){
srs_close_stfd(stfd);
return srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", server.c_str(), port);
}

*pstfd = stfd;
return srs_success;
}
Expand Down Expand Up @@ -248,7 +248,7 @@ srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd)
hints.ai_flags = AI_NUMERICHOST;

addrinfo* r = NULL;
SrsAutoFreeF(addrinfo, r);
SrsAutoFreeH(addrinfo, r, freeaddrinfo);
if(getaddrinfo(ip.c_str(), sport, (const addrinfo*)&hints, &r)) {
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "getaddrinfo hints=(%d,%d,%d)",
hints.ai_family, hints.ai_socktype, hints.ai_flags);
Expand All @@ -268,6 +268,7 @@ srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd)
return err;
}


srs_error_t do_srs_udp_listen(int fd, addrinfo* r, srs_netfd_t* pfd)
{
srs_error_t err = srs_success;
Expand Down Expand Up @@ -309,7 +310,7 @@ srs_error_t srs_udp_listen(std::string ip, int port, srs_netfd_t* pfd)
hints.ai_flags = AI_NUMERICHOST;

addrinfo* r = NULL;
SrsAutoFree(addrinfo, r);
SrsAutoFreeH(addrinfo, r, freeaddrinfo);
if(getaddrinfo(ip.c_str(), sport, (const addrinfo*)&hints, &r)) {
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "getaddrinfo hints=(%d,%d,%d)",
hints.ai_family, hints.ai_socktype, hints.ai_flags);
Expand All @@ -325,7 +326,7 @@ srs_error_t srs_udp_listen(std::string ip, int port, srs_netfd_t* pfd)
::close(fd);
return srs_error_wrap(err, "fd=%d", fd);
}

return err;
}

Expand Down