問(wèn)題描述
當(dāng)我查看 gdb 中的回溯時(shí),我的應(yīng)用程序有時(shí)會(huì)出現(xiàn)段錯(cuò)誤,主要是在 malloc() 和 malloc_consolidate() 中.
My application segfaults sometimes and mainly in malloc() and malloc_consolidate() when I look at the backtrace in gdb.
我確認(rèn)機(jī)器有足夠的可用內(nèi)存,它甚至沒(méi)有開(kāi)始交換.我檢查了數(shù)據(jù)段的 ulimits 和最大內(nèi)存大小,兩者都設(shè)置為無(wú)限制".我也在 valgrind 下運(yùn)行該應(yīng)用程序,沒(méi)有發(fā)現(xiàn)任何內(nèi)存錯(cuò)誤.
I verified that the machine has enough memory available, it didn't even start swapping. I checked ulimits for data segement and max memory size and both are set to 'unlimited'. I also ran the application under valgrind and didn't find any memory errors.
現(xiàn)在我不知道還有什么可能導(dǎo)致這些段錯(cuò)誤.有什么想法嗎?
Now I'm out of ideas what else might be causing these segfaults. Any Ideas ?
更新:由于我沒(méi)有找到任何與 valgrind(或 ptrcheck)有關(guān)的東西,是不是另一個(gè)應(yīng)用程序正在破壞 libc 的內(nèi)存結(jié)構(gòu),還是每個(gè)進(jìn)程都有一個(gè)單獨(dú)的結(jié)構(gòu)?
Update: Since I'm not finding anything with valgrind (or ptrcheck), could it be that another application is trashing libc's memory structure or is there a separate structure for each process ?
推薦答案
來(lái)自 http://www.gnu.org/s/libc/manual/html_node/Heap-Consistency-Checking.html#Heap-Consistency-Checking:
檢查和防范使用中的錯(cuò)誤的另一種可能性malloc、realloc和free是設(shè)置環(huán)境變量MALLOC_CHECK_.當(dāng) MALLOC_CHECK_ 被設(shè)置時(shí),一個(gè)特殊的(效率較低的)使用了旨在容忍簡(jiǎn)單的實(shí)現(xiàn)錯(cuò)誤,例如使用相同參數(shù)兩次調(diào)用 free,或單個(gè)字節(jié)的溢出(逐一錯(cuò)誤).并非所有此類錯(cuò)誤都可以但是,可能會(huì)導(dǎo)致內(nèi)存泄漏.如果MALLOC_CHECK_ 設(shè)置為 0,任何檢測(cè)到的堆損壞都是靜默的忽略;如果設(shè)置為 1,則在 stderr 上打印診斷信息;如果設(shè)置為 2,立即調(diào)用 abort.這可能很有用,因?yàn)榉駝t崩潰可能發(fā)生得更晚,問(wèn)題的真正原因是然后很難追查.
Another possibility to check for and guard against bugs in the use of malloc, realloc and free is to set the environment variable MALLOC_CHECK_. When MALLOC_CHECK_ is set, a special (less efficient) implementation is used which is designed to be tolerant against simple errors, such as double calls of free with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be protected against, however, and memory leaks can result. If MALLOC_CHECK_ is set to 0, any detected heap corruption is silently ignored; if set to 1, a diagnostic is printed on stderr; if set to 2, abort is called immediately. This can be useful because otherwise a crash may happen much later, and the true cause for the problem is then very hard to track down.
這篇關(guān)于malloc() 和 malloc_consolidate() 中的段錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!