Home > Archive > Unix Programming > June 2004 > core dumped with malloc
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
core dumped with malloc
|
|
| vagos 2004-05-28, 12:24 pm |
| Hi all,
I have a bug I cannot understand why it happens.
There is a segmantation fault exactly on a call loke this:
"char *str = (char *)malloc(100);"
There is a function in the code that have some local variables initiated as above. The memory is set free before exiting the funciton.
The problem is that I have the segmentation fault NOT ALWAYS and not on the same local variable. The code may burst on the 5th, 6th call of the funciton and where allocating memory on the 3rd, 4th local variable. It is not deterministic. I have tried with -dmalloc,
-efence etc but I have no result yet.
The only thing that crosses my mind is that maybe it has something to do with threads, because this function is called by different threads. I have used the <pthread> library. But I knowthat libc is safe thread, so malloc should not have problem.
Since I only do some "malloc"s and then the respective "free"s, what happens and the SIGSERV is sent and with different call stack each time? Does anyone have any idea?
Thanks,
Vaggelis | |
| Alexander 2004-05-31, 6:05 pm |
| use gdb to find exactly the line the seg fault occurs.
1)gcc -ggdb -o yourprogram yourprogram.c
2)gdb yourprogram
3) run (its a gdb command, type in the gdb shell, parameters may be passed after run as to usual program)
4) interact with program, make it seg fault
5) When sig fault will occur it will show you the line in your program when something illigal is happening
-----------------------------------------------------------------
Most probably it will show you some giberish position, that means that you probably called free() on a pointer that already disposed of by another free() you called. Every free() must match malloc(), otherwise you'll be screwed. | |
| vagos 2004-06-01, 10:53 am |
| I have already tried with GDB, I have also tried by retrieving the call stack by the core files. The SIGSERV is thrown at one of the malloc calls (never the free) non deterministic. I cannot locate the exact line, it is always a DIFFERENT one!!!!!!
The function does 5 malloc when it begins and 5 free in the end. It is too simple to be wrong or forget something.
Anyway, thanks | |
| Alexander 2004-06-01, 2:07 pm |
| quote: Originally posted by vagos
I have already tried with GDB, I have also tried by retrieving the call stack by the core files. The SIGSERV is thrown at one of the malloc calls (never the free) non deterministic. I cannot locate the exact line, it is always a DIFFERENT one!!!!!!
The function does 5 malloc when it begins and 5 free in the end. It is too simple to be wrong or forget something.
Anyway, thanks
Just write your code in here |
|
|
|
|