含有读写锁的结构无法释放

baidu_33255168 2015-12-22 09:12:25
编写了一个含有读写锁pthread_rwlock_t的结构,对该结构进行了初始化,并对读写锁结构成员进行了初始化pthread_rwlock_init(),之后销毁读写锁成员,再去释放该结构就不行了,报出core dump。
再注销掉读写锁结构成员初始化一行,在释放结构就行了,为什么会这样?
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-23
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
baidu_33255168 2015-12-23
  • 打赏
  • 举报
回复
qp = (struct myqueue*)calloc(1,sizeof(struct myqueue)); ========不是======= qp = (struct myqueue*)calloc(1,sizeof(struct myqueue*)); 继续请教,两者有什么区别,编译都能通过啊?
fefe82 2015-12-23
  • 打赏
  • 举报
回复
qp = (struct myqueue*)calloc(1,sizeof(struct myqueue)); ========不是======= qp = (struct myqueue*)calloc(1,sizeof(struct myqueue*));
baidu_33255168 2015-12-23
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


pthread_t tid1,tid2,tid3,tid4;

struct job{
    struct job *prev;
    struct job *next;
    pthread_t id;
};

struct myqueue{
    struct job *head;
    struct job *tail;
    pthread_rwlock_t qlock;  /* protect head and tail pointer */
};

struct myqueue *qp;
struct job *job1,*job2,*job3,*job4;

int init_queue()
{
    int err;
    
    /* initializing work queue*/	
    qp = (struct myqueue*)calloc(1,sizeof(struct myqueue*)); /* calloc(): clean up */
    err = pthread_rwlock_init(&qp->qlock, NULL); /* 注释掉该行,可以释放qp */
    if(err != 0)
    	return err;
    
    /* initializing jobs */    
    job1 = (struct job*)calloc(1,sizeof(struct job*));
    job2 = (struct job*)calloc(1,sizeof(struct job*));
    job3 = (struct job*)calloc(1,sizeof(struct job*));
    job4 = (struct job*)calloc(1,sizeof(struct job*));	
}


int main()
{
    /* 1.initializing queue and jobs */
    init_queue();    
        
    pthread_rwlock_destroy(&qp->qlock);	
    free(qp);  /* 该行出现问题 */  
    
    return 0;
}
fefe82 2015-12-22
  • 打赏
  • 举报
回复
提供一下程序吧 ...

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧