On the verse of memory leaks
This commit is contained in:
+16
-6
@@ -31,8 +31,9 @@ int main(int argc, char **argv){
|
||||
while(thread_count < worker_count){
|
||||
int *port = malloc(sizeof(int));
|
||||
*port = atoi(argv[worker_index]);
|
||||
if(pthread_create(&threads[thread_count++], NULL, workerFunc, port) != 0){
|
||||
if(pthread_create(&threads[thread_count++], NULL, workerFunc, (void*)port) != 0){
|
||||
perror("[MAIN] Failed to create thread");
|
||||
free(port);
|
||||
free(threads);
|
||||
exit(1);
|
||||
}
|
||||
@@ -65,8 +66,13 @@ TYPE get_request_type(const char *request){
|
||||
}
|
||||
|
||||
void* workerFunc(void *args){
|
||||
if(!args){
|
||||
perror("[THREAD] Invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
int port = *(int*)args;
|
||||
free(args);
|
||||
|
||||
void *context = zmq_ctx_new();
|
||||
if(!context){
|
||||
perror("[THREAD] Failed to create context");
|
||||
@@ -94,10 +100,10 @@ void* workerFunc(void *args){
|
||||
key_value_pair *head = NULL;
|
||||
key_value_pair *tail = NULL;
|
||||
|
||||
char request[MESSAGE_LENGTH + 1];
|
||||
char request[MESSAGE_LENGTH + 1] = {0};
|
||||
int bytes_recv = zmq_recv(responder, request, MESSAGE_LENGTH, 0);
|
||||
if(bytes_recv <= 0){
|
||||
perror("Failed to receive request");
|
||||
if(bytes_recv < 3){ // minimal receive is TYPE
|
||||
perror("Failed to receive valid request");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -106,9 +112,11 @@ void* workerFunc(void *args){
|
||||
char endpoint[256];
|
||||
size_t endpoint_size = sizeof(endpoint);
|
||||
if (zmq_getsockopt(responder, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_size) == 0) {
|
||||
fprintf(stderr, "Got request from: %s\n", endpoint);
|
||||
fprintf(stderr, "Got request on: %s\n", endpoint);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Received: %s\n", request);
|
||||
|
||||
const TYPE type = get_request_type(request);
|
||||
char *content = request + TYPE_LENGTH;
|
||||
|
||||
@@ -137,6 +145,7 @@ void* workerFunc(void *args){
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Sending content: %s\n", ret);
|
||||
if(zmq_send(responder, ret, strlen(ret), 0) < 0){
|
||||
perror("Failed to send response");
|
||||
@@ -178,7 +187,7 @@ char* get_word(const char *request){
|
||||
return NULL;
|
||||
|
||||
while(*request && isalpha(*request)){
|
||||
char *new_word = (char*)realloc(word, strlen(word) + 2); // realloc word size + \0 and extra char space
|
||||
char *new_word = (char*)realloc(word, length + 2); // realloc word size + \0 and extra char space
|
||||
if(!new_word){
|
||||
free(word);
|
||||
return NULL;
|
||||
@@ -193,6 +202,7 @@ char* get_word(const char *request){
|
||||
}
|
||||
|
||||
char* map(const char *content, key_value_pair **head, key_value_pair **tail){
|
||||
if(!content) return NULL;
|
||||
char *content_iterator = (char*)content;
|
||||
void *content_start_address = (void*)content;
|
||||
size_t content_length = strlen(content);
|
||||
|
||||
Reference in New Issue
Block a user