1
void *insert_rear_node(void *arg)
{   
  int *argument=(int *)arg;
  int value=*argument;
  //Assume that allocation is happening fine (Just like malloc , it is a custom heap allocator)

  struct Node *head=(struct Node *) RTallocate(RTpointers, sizeof(struct Node));
  struct Node *temp;
  setf_init(temp,head);

  while(value>0)
    {
      if(temp==NULL)
        {
          RTwrite_barrier(&(temp), new_node(value,NULL));
          RTwrite_barrier(&(head),temp);
          printf("Inserted %d into the list\n",head->data);
        }
      else
        {
          RTwrite_barrier(&(temp),head);
          while(temp->next!=NULL)
            {
              RTwrite_barrier(&(temp),temp->next);
            }
          RTwrite_barrier(&(temp->next),new_node(value,NULL));
          printf("Inserted %d into the list\n",temp->next->data);
        }
      value=value-1;

    }
  free(head);

}

int main(int argc, char *argv[])
{                                                                                                                                                                                                                              
  long heap_size = (1L << 28);
  long static_size = (1L << 26);
  printf("heap_size is %ld\n", heap_size);
  RTinit_heap(heap_size, static_size, 0);                                                                                                                                                                                                                 
  pthread_t thread1;                                                                                                                                                                                                                                   
  int limit=1000;
  RTpthread_create(&thread1,NULL,insert_rear_node,(void *)&limit);
}

假设 RTallocate 和 RTwrite_barrier 是 2 个自定义函数,它们工作得很好。RTallocate - 在堆上分配内存 RTwrite_barrier 相当于一个assignemnt 语句。

该程序只是将节点插入到链表中。然后尝试删除头部。

我收到此错误:
`/home/test/RT-Test/InMul' 中的错误:双重释放或损坏(输出):0x00007fffe3465010
警告:损坏的共享库列表:0x7ffea4000920 != 0x7ffff7ffd9d8
======= 回溯:= ========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff77f97e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7ffff7801e0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff780598c]
/home/test/RT-Test/InMul[0x400b98]
/lib/x86_64-linux-gnu/librtgc.so(rtalloc_start_thread+ 0x1ef)[0x7ffff7b4fa2c] /lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7ffff756c6ba] /lib/x86_64-linux-gnu/libc.so.6(克隆+0x6d)[0x7ffff788882d]


我只释放一次头部。为什么我会面临这个问题?
0x00007fffe3465010 是头部的地址。

4

2 回答 2

1

您没有向我们展示的某些代码(很可能与您认为有问题的代码相去甚远)损坏了malloc的内部簿记数据。

在下运行您的程序valgrind并修复它报告的第一个无效操作。重复直到 valgrind 没有更多的抱怨。现在你的程序应该可以工作了。如果您不理解 valgrind 的输出,请将它的20 行左右编辑到您的问题中,我们也许可以解释。

于 2017-06-15T19:04:07.070 回答
1

当你没有使用 malloc() 作为 head 时,你不能使用 free()。使用与 RTallocate 等效的免费版本。

于 2017-06-15T18:47:40.280 回答