I received a double free or corruption (fasttop) on the code below. Can't seem to figure out which pointer is creating this error. It looks like the error is being produced in the while loop of the echo_cnt function. Thanks!
static void init_echo_cnt(void) {
sem_init(&mutex, 0, 1);
byte_cnt = 0;
}
void echo_cnt(int connfd) {
int n;
FILE *fp = fopen("words.txt", "r");
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, init_echo_cnt);
char *buf = calloc(MAXLINE, sizeof(char));
while((n=readLine(connfd, buf, MAXLINE)) != 0) {
char *str = fgets(buf, sizeof buf, fp);
char *token;
token = strtok(str, " ");
char *result = malloc(strlen(token +64));
while (token != NULL){
printf("In the while loop\n");
if (spellcheck(token, fp)==1){
strcat(result, token);
strcat(result, "OK");
}
else{
strcat(result, "MISPELLED");
}
ssize_t kick = write(connfd, result, MAXLINE);
free(result); }
}
close(connfd);
}
void *thread(void *vargp) {
int connfd;
pthread_detach(pthread_self());
printf("World\n");
while(1) {
connfd=sbuf_remove(sbuf); //remove socket
echo_cnt(connfd); //service client
}
close(connfd); //close socket
}