简体   繁体   中英

*** glibc detected *** free(): invalid pointer

I have the following code that produces a *** glibc detected *** free(): invalid pointer error whenever I run the code.

main.h :

#ifndef PTHREAD_CALC_H_
#define PTHREAD_CALC_H_

void* task(void*);

#endif

main.cxx :

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include "main.h"

int main(int argc, char* argv[]) {

    pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t)*2);
    double  *temp;

    double sum = 0.0;
    for (int j = 0; j < 2; j++) {
        pthread_create(&(threads[j]), NULL, task, NULL);
    }

    for (int j = 0; j < 2; j++) {
        pthread_join(threads[j], (void**)(&temp));
        sum += *temp;
    }

    free(threads);
    free(temp);

    return 0;
}

void* task(void *data) {
    double sum = 5;
    pthread_exit((void*)&sum);
    return NULL;
}

I'm having a hard time determining what is causing the error. Any assistance is greatly appreciated. If I can provide anything else to help pinpoint the problem, please let me know.

Thank you

Edit

For sake of completion, here is the resulting code that executes as expected:

main.cxx :

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include "main.h"

int main(int argc, char* argv[]) {

    pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t)*2);
    double  *temp;

    double sum = 0.0;
    for (int j = 0; j < 2; j++) {
        pthread_create(&(threads[j]), NULL, task, NULL);
    }

    for (int j = 0; j < 2; j++) {
        pthread_join(threads[j], (void**)&temp);
        sum += temp;
        delete temp;
    }

    free(threads);
    return 0;
}

void* task(void *data) {
    double* sum = new double;
    *sum = 5.0;
    pthread_exit(static_cast<void*>(sum));
}

Currently your thread task returns some value on the stack of the thread. When the thread finishes there is no guarantee that *temp will point to something valid.

Thus after this call

pthread_join(threads[j], (void**)(&temp));

temp points to the old location of sum in the thread, but if the thread finishes it doesn't exists anymore. And using it will result in undefined behavior.

yet later you free temp which points to the double on the other stack, but there is noting to free since stack allocations are free automatically when they go out of scope.

free(temp);

want you might want to do is:

void* task(void *data) {
    double* sum = new double;
    *sum = 5;
    pthread_exit(static_cast<void*>(sum) );
}

and then in main after joining the thread

delete temp;

In your code you declare:

double  *temp;

You never malloc to the address, but later you free it. This will produce the error. Remove the free(temp) and it should work. Actually though you've introduced new errors by dereferencing *temp without storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM