linux 线程 内存释放,多线程内存释放的问题?

本帖最后由 zhoukunta 于 2009-06-22 11:59:05 编辑

我用父线程函数创建线程,并为每一个子线程传递一个thread_date结构的已分配空间的地址,在子线程中进行释放,如红色区域所示,我释放的方法换了好多(如红色所示),但释放的时候就会报错,感觉我传递的地址没有问题啊,我都用打印信息看了,在子线程中的释放参数arg/date的地址与父线程中申请的地址是一样的,各个线程地址也不重复,但不知道为什么,释放就出问题。去掉就可以,难道是系统自动释放了?结构里是连个string类型。

待传递给子线程的数据结构

struct thread_date

{

string ip;

string path;

};

父线程函数创建线程

int DeleteMultiThread(string watchbuf,string pathdes,remote_id *rid,int ridnum)

{

...

for(int i=0;iip=rid[i].ip;

ip_path->path=pathdes;

int res = pthread_create(&(a_thread[i]),&thread_attr,ClientThread,  (void*)ip_path);

}

return 0;

}

被调用的子线程

******************************************************************************/

void *ClientThread(void *arg)

{

thread_date* date = (thread_date*)arg;

const char* path = (date->path).c_str();

const char* ip   = (date->ip).c_str();

printf("ClientThread is running. Argument was %sn",(date->ip).c_str());

delete arg;//delete []arg; delete []date delete date free(arg) free(date)

pthread_exit(NULL);

}//end function

|

thread_date *ip_path = new thread_date[sizeof(thread_date)];

这句有问题 这样声明是开辟了一个thread_date的数组,大小是sizeof(thread_date)

改成thread_date *ip_path = new thread_date;

或者

thread_date *ip_path = (thread_date *)malloc(sizeof(thread_date))

|

delete arg换成delete []date

|

delete释放数组用在后面加[]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值