使用 strtok_r 时 malloc.c 断言失败
我用C语言(Ubuntu 10.10)制作了一个UDP服务器。服务器相当大,其中一部分使用 Strtok_r() 进行一些字符串处理[我之前已经成功使用过这个函数]。当服务器第一次运行时,它会正确处理来自客户端的数据。但是,当另一个客户端出现并发送一些数据时,程序崩溃并显示以下消息:
MappingServer: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
如果我注释掉 strtok_r() 函数,则一切正常(无论有多少客户端来!)。导致问题的代码是:
char delims[] = "/";
char* token = NULL;
char* separated_token[4];
int i,j;
char* last;
j = 0;
token = strtok_r( input_string, delims, &last );
while( token != NULL)
{
separated_token[j] = malloc(strlen(token) + 1);
strcpy(separated_token[j],token);
printf("%s ", separated_token[j] );
j++;
token = strtok_r( NULL, delims, &last );
}
据我所知,代码没问题,并且对于第一个客户端来说工作得很好。我有点困惑,这个错误是什么意思?我尝试过使用 strtok() ,结果是相同的。
I have made an UDP server in C (Ubuntu 10.10). The server is quite large and part of it does some String processing using Strtok_r() [I have used this function successfully before]. When the server runs for the first time, it processes the data from the client correctly. But when another client comes along and sends some data, the program crashes with the following message:
MappingServer: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
If I comment out the strtok_r() function then everything works properly (No matter how many clients r coming!). The code that's causing the problem is:
char delims[] = "/";
char* token = NULL;
char* separated_token[4];
int i,j;
char* last;
j = 0;
token = strtok_r( input_string, delims, &last );
while( token != NULL)
{
separated_token[j] = malloc(strlen(token) + 1);
strcpy(separated_token[j],token);
printf("%s ", separated_token[j] );
j++;
token = strtok_r( NULL, delims, &last );
}
As far as I can understand, the code is OK and it works perfectly for the first client. I am a little confused, what does this error mean? I have tried with strtok() and the result is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着堆中的内部 malloc 结构被破坏。尝试在 valgrind 中运行服务器以查找堆使用错误和/或内存损坏。
您添加的代码很难分析,因为没有代码会执行
free()
;separated_token[]
数组的大小未知。处理后是否 free() 所有令牌?您是否为separated_token[]
数组本身分配了足够的元素?在开始标记第二个请求之前,您是否将j
计数器重置为零?This means that internal malloc structures in the heap are broken. Try to run you server in the
valgrind
to find heap usage error and/or memory corruption.The code you added is hard to analyse, because there is not code with will do a
free()
; the size ofseparated_token[]
array is not known. Do you free() all tokens after processing? Do you allocate enough elements for theseparated_token[]
array itself? Do you resetj
counter to zero before start tokenizing second request?