hi vaidya,
Use "electric fence" while linking your program and verify that the segmentation fault is due to free() call. It is possible that some illegal memory is being accessed before the free() call. Programs not linked with electric fence may not stop at the exact instruction where seg fault occured. It is very much possible that your debugger (like gdb) may stop at free() which makes you feel that the seg fault is due to the free() call.
To link with electric fence, add "-lefence" at the link command.
regards, Rajeev -- All you need is ignorance and confidence: then success is sure - Mark Twain
vaidya wrote:
Hi,
Suppose I do a malloc and store some data in the memory allocated. Now when I'm freeing this memory, I'm getting a segmentation fault.
I've ensured that this memory chunk is not freed before and that the pointer is also ok.
What I suspect is that the bytes just before this memory chunk is being tampered with and hence is leading to the segmentation fault when being freed.
Could you please give me some pointers as to how I can learn about the way linux libraries go about malloc ing memory and what other extra data does it store and how and where.
Thanks in advance
vaidya - : send the line "unsubscribe linux-net" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
- : send the line "unsubscribe linux-net" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
You can also set MALLOC_CHECK_=[1|2] in your environment prior to running your program. The libc malloc implementation has some bulit in checks that it can preform for you to detect some forms of heap corruption. man malloc for details.
Neil
-- /*************************************************** *Neil Horman *Software Engineer *Red Hat, Inc. *nhorman@redhat.com *gpg keyid: 1024D / 0x92A74FA1 *http://pgp.mit.edu ***************************************************/ - : send the line "unsubscribe linux-net" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html