简体   繁体   English

***检测到glibc *** free():无效的指针

[英]*** glibc detected *** free(): invalid pointer

I have the following code that produces a *** glibc detected *** free(): invalid pointer error whenever I run the code. 我有以下代码会生成*** glibc detected *** free(): invalid pointer*** glibc detected *** free(): invalid pointer每当我运行代码时,都会发生*** glibc detected *** free(): invalid pointer错误。

main.h : main.h

#ifndef PTHREAD_CALC_H_
#define PTHREAD_CALC_H_

void* task(void*);

#endif

main.cxx : 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 : 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. 当线程完成时,不能保证* temp指向有效的东西。

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. temp指向线程中sum的旧位置,但是如果线程结束,它将不再存在。 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. 后来,您释放了temp,该温度指向另一个堆栈上的双精度型,但由于堆栈分配超出范围,堆栈分配是自动释放的,因此请注意不要释放。

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. 您永远不会将malloc分配给该地址,而是稍后将其释放。 This will produce the error. 这将产生错误。 Remove the free(temp) and it should work. 删除free(temp),它应该可以工作。 Actually though you've introduced new errors by dereferencing *temp without storage. 实际上,尽管您通过取消引用* temp而没有存储引入了新的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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