Remove all server memory leaks on shutdown

This commit is contained in:
akp 2023-11-11 18:50:19 +00:00
parent c4b7495852
commit 19776081b8
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
3 changed files with 37 additions and 4 deletions

View file

@ -22,4 +22,4 @@ echo
./form_message.py D 192.168.0.0-192.168.0.255 80 | nc 127.0.0.1 $PORT && echo
./form_message.py D 68.108.144.151 40-4000 | nc 127.0.0.1 $PORT && echo
./form_message.py D 127.0.0.1 700 | nc 127.0.0.1 $PORT && echo
./form_message.py D 127.0.0.1 700 | nc 127.0.0.1 $PORT && echo

View file

@ -50,11 +50,13 @@ int main (int argc, char **argv) {
signal(SIGINT, &sigint_handler);
int res, status;
if ((status = pthread_join(thread_id, (void *) &res)) != 0) {
int socket_res, status;
if ((status = pthread_join(thread_id, (void *) &socket_res)) != 0) {
fprintf(stderr, "join worker thread: %s\n", strerror(status));
return 1;
}
return res;
teardown_frames();
return socket_res;
}

View file

@ -250,4 +250,35 @@ int delete_frame(struct Rule* rule) {
pthread_rwlock_unlock(&persist_lock);
return hasChanged;
}
void teardown_frames() {
pthread_rwlock_wrlock(&persist_lock);
struct Frame *frame_cursor = persist_head;
while (frame_cursor != NULL) {
struct Frame *next = frame_cursor->next;
struct LogFrame *log_cursor = frame_cursor->log;
while (log_cursor != NULL) {
struct LogFrame *next = log_cursor->next;
free(log_cursor);
log_cursor = next;
}
if (frame_cursor->rule.ip.end != NULL) {
free(frame_cursor->rule.ip.end);
}
if (frame_cursor->rule.port.end != NULL) {
free(frame_cursor->rule.port.end);
}
free(frame_cursor);
frame_cursor = next;
}
pthread_rwlock_unlock(&persist_lock);
}