My Chatbot polling feature is not working properly, I'm getting segmentation error on a thread handler, i used gdb to see more stuff and here there is what i got:
Thread 4 "St3veB0t" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff67c2700 (LWP 5957)]
0x0000555555557243 in poll_handler ()
(gdb) bt
#0  0x0000555555557243 in poll_handler ()
#1  0x00007ffff7bbd6db in start_thread (arg=0x7ffff67c2700)
    at pthread_create.c:463
#2  0x00007ffff78e688f in clone ()
    at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
The function poll_handler() is this:
void * poll_handler(void * args)
{
    FILE * fp;
    struct VoteData  vote;
    struct PollHandlerData * data = (struct PollHandlerData *)args;
    int res;
    char * string = (char *)malloc(sizeof(char)*MAX_BUFFER);
    sleep(60);
    *data->status = 0;    
    *data->vote_count = 0;
    if(!(fp = fopen("polls/votes.txt", "r")))
    {
        fprintf(stderr, "\nError in reading file\n");
         if(!(fp = fopen("polls/votes.txt", "w+")))
         {
            fprintf(stderr, "\nError in creating file\n");
            exit(EXIT_FAILURE);
         }
    }
    vote = GetMostVote(fp);
    strcpy(string, "PRIVMSG #st3ver0nix : Polling terminated, the majority voted: ");
    strcat(string, vote.word);
    strcat(string, "\r\n");
     do{
        res = write(data->sock, string, strlen(string));
    }while(res < strlen(string));
    fclose(fp);
    free(string);
    pthread_exit(NULL);
}
The function that creates the thread is this:
void CreatePoll(int sock, char * message, char * poll_name, int * status, int * vote_count)
{
    pthread_t tid;
    struct PollHandlerData * data = (struct PollHandlerData *)malloc(sizeof(struct PollHandlerData));
    char * name = (char *)malloc(sizeof(char)*MAX_BUFFER);
    GetPollName(message, name);
    sscanf(name, "%s", poll_name);
    data->sock = sock;
    data->status = status;
    sscanf(poll_name, "%s", data->name);
    pthread_create(&tid, NULL, poll_handler, (void *)data);
    pthread_detach(tid);
    free(name);
}
The structures PollHandlerData and VoteData have this form:
struct PollHandlerData
{
    int sock;
    char name[128];
    int * status;
    int * vote_count;
};
struct VoteData
{
    char word[128];
    int freq;
};
I really don't know what's wrong in my code. I'm using POSIX pthreads. Pls let me know if you need more information about the code.
 
    