-
Notifications
You must be signed in to change notification settings - Fork 220
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
Fix memory leak in rtl_tcp on TCP client close #5
Open
benschumacher
wants to merge
1
commit into
pinkavaj:master
Choose a base branch
from
benschumacher:rtltcp_memory_leak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdatomic.h> | ||
#include <stdbool.h> | ||
|
||
#ifndef _WIN32 | ||
#include <unistd.h> | ||
|
@@ -92,11 +94,12 @@ static int verbosity = 0; | |
static uint32_t bandwidth = 0; | ||
|
||
static int enable_biastee = 0; | ||
static int global_numq = 0; | ||
static atomic_int global_numq = 0; | ||
static struct llist *ll_buffers = 0; | ||
static int llbuf_num = 500; | ||
|
||
static volatile int do_exit = 0; | ||
atomic_bool do_exit = false; | ||
atomic_bool conn_reset = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this one be |
||
|
||
void usage(void) | ||
{ | ||
|
@@ -158,7 +161,7 @@ sighandler(int signum) | |
{ | ||
if (CTRL_C_EVENT == signum) { | ||
fprintf(stderr, "Signal caught, exiting!\n"); | ||
do_exit = 1; | ||
do_exit = true; | ||
rtlsdr_cancel_async(dev); | ||
return TRUE; | ||
} | ||
|
@@ -167,15 +170,20 @@ sighandler(int signum) | |
#else | ||
static void sighandler(int signum) | ||
{ | ||
fprintf(stderr, "Signal caught, exiting!\n"); | ||
fprintf(stderr, "Signal (%d) caught, exiting!\n", signum); | ||
do_exit = true; | ||
rtlsdr_cancel_async(dev); | ||
do_exit = 1; | ||
} | ||
#endif | ||
|
||
static bool may_continue_(void) | ||
{ | ||
return !conn_reset && !do_exit; | ||
} | ||
|
||
void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) | ||
{ | ||
if(!do_exit) { | ||
if(may_continue_()) { | ||
struct llist *rpt = (struct llist*)malloc(sizeof(struct llist)); | ||
rpt->data = (char*)malloc(len); | ||
memcpy(rpt->data, buf, len); | ||
|
@@ -232,7 +240,7 @@ static void *tcp_worker(void *arg) | |
int r = 0; | ||
|
||
while(1) { | ||
if(do_exit) | ||
if(!may_continue_()) | ||
pthread_exit(0); | ||
|
||
pthread_mutex_lock(&ll_mutex); | ||
|
@@ -266,10 +274,18 @@ static void *tcp_worker(void *arg) | |
bytesleft -= bytessent; | ||
index += bytessent; | ||
} | ||
if(bytessent == SOCKET_ERROR || do_exit) { | ||
printf("worker socket bye\n"); | ||
sighandler(0); | ||
pthread_exit(NULL); | ||
if(bytessent == SOCKET_ERROR || !may_continue_()) { | ||
// cleanup buffers for this socket | ||
while (curelem != NULL) { | ||
prev = curelem; | ||
curelem = curelem->next; | ||
free(prev->data); | ||
free(prev); | ||
} | ||
printf("worker socket bye\n"); | ||
conn_reset = true; | ||
rtlsdr_cancel_async(dev); | ||
pthread_exit(NULL); | ||
} | ||
} | ||
prev = curelem; | ||
|
@@ -388,9 +404,10 @@ static void *command_worker(void *arg) | |
} | ||
fflush(stdout); | ||
} | ||
if(received == SOCKET_ERROR || do_exit) { | ||
if(received == SOCKET_ERROR || !may_continue_()) { | ||
printf("comm recv bye\n"); | ||
sighandler(0); | ||
conn_reset = true; | ||
rtlsdr_cancel_async(dev); | ||
pthread_exit(NULL); | ||
} | ||
} | ||
|
@@ -785,6 +802,7 @@ int main(int argc, char **argv) | |
i = WSAStartup(MAKEWORD(2,2), &wsd); | ||
#else | ||
struct sigaction sigact, sigign; | ||
memset(&sigign, 0, sizeof(struct sigaction)); | ||
#endif | ||
|
||
opt_str = "a:p:f:g:s:b:n:d:P:O:TI:W:l:w:D:vr:"; | ||
|
@@ -989,7 +1007,7 @@ int main(int argc, char **argv) | |
r = fcntl(listensocket, F_SETFL, r | O_NONBLOCK); | ||
#endif | ||
|
||
while(1) { | ||
while(!do_exit) { | ||
printf("listening...\n"); | ||
printf("Use the device argument 'rtl_tcp=%s:%d' in OsmoSDR " | ||
"(gr-osmosdr) source\n" | ||
|
@@ -1004,9 +1022,7 @@ int main(int argc, char **argv) | |
tv.tv_sec = 1; | ||
tv.tv_usec = 0; | ||
r = select(listensocket+1, &readfds, NULL, NULL, &tv); | ||
if(do_exit) { | ||
goto out; | ||
} else if(r) { | ||
if(r) { | ||
rlen = sizeof(remote); | ||
s = accept(listensocket,(struct sockaddr *)&remote, &rlen); | ||
break; | ||
|
@@ -1063,11 +1079,10 @@ int main(int argc, char **argv) | |
free(prev); | ||
} | ||
|
||
do_exit = 0; | ||
conn_reset = false; | ||
global_numq = 0; | ||
} | ||
|
||
out: | ||
rtlsdr_close(dev); | ||
closesocket(listensocket); | ||
/* if (port_ir) pthread_join(thread_ir, &status); */ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the static is remove? It looks like the variable is still only used within this file.