【问题标题】:glibc detected free() while using posix threads in Cglibc 在 C 中使用 posix 线程时检测到 free()
【发布时间】:2012-10-11 08:35:08
【问题描述】:

我不断收到此“glibc 检测到 free(): invalid next size (fast)”错误,但不知道具体原因。我读到它是因为出现越界错误,但在我的代码中没有看到可能发生这种情况的任何地方,有没有人看到我错过的任何东西?

这是我的代码:

typedef struct
{
int* inputData;
int* histogramData;
int numElements;
pthread_t* tid;
} threadInput;


void* threadRoutine(void* argv)
{

// initializers
int i, avgInputSize, lastInputSize, threadStart, threadEnd, threadNum, numThreadsUsed;

// stores input data into tempData
threadInput* tempData = (threadInput*) argv;

// calculates the required number of threads
numThreadsUsed = NUM_THREADS; 
if(NUM_THREADS > tempData->numElements) 
{
    numThreadsUsed = tempData->numElements;
}


// create histogram
for(threadNum = 0; threadNum < numThreadsUsed; threadNum++)
{
    if(tempData->tid[i] == pthread_self())
    {

        // finds start and end of data set for thread
        if(tempData->numElements > numThreadsUsed)
        {
            avgInputSize = (int)((tempData->numElements)/NUM_THREADS);
            threadStart = threadNum*avgInputSize;
            if(i < (NUM_THREADS-1)) 
            {
                threadEnd = ((threadNum+1)*avgInputSize);
            }
            else if(i == (NUM_THREADS-1)) 
            {
                threadEnd = (tempData->numElements);        
            }
        }
        else
        {
            threadStart = i;
            threadEnd = i + 1;
        }


        // creates histogram
        pthread_mutex_lock(&lock);

        for(i = threadStart; i < threadEnd; i++)
        {
            tempData->histogramData[tempData->inputData[i]]++;
        }

        pthread_mutex_unlock(&lock);
    }
}


pthread_exit(0);
}


void compute_using_pthreads(int *input_data, int *histogram, int num_elements, int histogram_size)
    {
    // initializers
    int i, j;
    threadInput* input = malloc(sizeof(threadInput*));
    input->inputData = malloc(sizeof(input_data));
    input->histogramData = malloc(sizeof(histogram));
input->tid = malloc(NUM_THREADS*sizeof(pthread_t));

// enters data into struct
    input->inputData = input_data;
input->histogramData = histogram;

// Create threads
    for(i = 0; i <  NUM_THREADS; i++)
            pthread_create(&input->tid[i], NULL, threadRoutine, (void*) &input);

// reaps threads
    for(i = 0; i <  NUM_THREADS; i++)
            pthread_join(input->tid[i], NULL);

    pthread_mutex_destroy(&lock);

    // frees space
    free(input->inputData);
    free(input->histogramData);
    free(input->tid);
    free(input);


}

【问题讨论】:

  • 在调试器下运行时会在哪一行出错?
  • threadInput.numElements 永远不会被填满。

标签: c multithreading posix free glibc


【解决方案1】:

这是一个错误:

threadInput* input = malloc(sizeof(threadInput*));

因为它只为threadInput* 分配了足够的空间,所以它应该分配给threadInput

threadInput* input = malloc(sizeof(*input));

input_datahistogram 的类似错误在于分配的是 sizeof(int*) 而不是 sizeof(int)

【讨论】:

    猜你喜欢
    • 2011-07-01
    • 2013-08-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多