开发者

malloc.c assertion failed while working with strtok_r

开发者 https://www.devze.com 2023-03-15 06:40 出处:网络
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 ru

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 f开发者_高级运维ar 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.


 malloc.c:3096: sYSMALLOc: Assertion

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 of separated_token[] array is not known. Do you free() all tokens after processing? Do you allocate enough elements for the separated_token[] array itself? Do you reset j counter to zero before start tokenizing second request?

0

精彩评论

暂无评论...
验证码 换一张
取 消