noaH
noaH

Reputation: 35

Can't free() a char*

void* password_cracker_thread(void* args) {
    cracker_args* arg_struct = (cracker_args*) args;
    md5hash* new_hash = malloc (sizeof(md5hash));


    while(1)
    {
        char* password = fetch(arg_struct->in);

        if(password == NULL  )
        {
            deposit(arg_struct->out,NULL);
            free(new_hash);
            pthread_exit(NULL);
        }
        compute_hash(password,new_hash);
        if(compare_hashes(new_hash,(md5hash**)arg_struct->hashes,arg_struct->num_hashes) != -1)
        {
            printf("VALID_PASS:%s \n",password);
            deposit(arg_struct->out,password);
        }else{
            free(password);

        }
    }

}

This is a part of a program, where you get char* passwords from a ringbuffer, calculate md5 and compare them and push them into the next buffer if valid.

My problem is now, why can't I free those I don't need? The whole program will stop if I try to and if I don't, I get memory leaks.

Upvotes: 0

Views: 237

Answers (1)

mlp
mlp

Reputation: 825

"You", and by this I mean your program, can only free() storage that was got from malloc()-and-friends, and only in the granularity it was got, and only once per chunk of storage.

In the code you show here, you're attempting to free() something got from fetch(). Since we can't see the definition of that function and you have not provided any documentation of it, our best guess is that

  • fetch() gives you a pointer to something other than a whole chunk got from malloc()-et-al; and/or
  • some other part of the program not shown here free()s the relevant chunk itself.

Upvotes: 1

Related Questions