localtime() 中的 malloc() 段错误? [英] malloc() seg fault in localtime()?
问题描述
这是 ny 堆栈
malloc() at 0xb7dfd333
strdup() at 0xb7e01866
tzset_internal() at 0xb7e2ef68
__tz_convert() at 0xb7e2f26a
localtime() at 0xb7e2d901
Send_Trace() at my_trace.c:265 0x8053373
这是有问题的代码..
and here's the offending code ..
void Send_Trace(const char const *Trace_Text, ...)
{
time_t time_now = time(NULL);
tm = *localtime(&time_now);
它通常工作正常,但偶尔会抛出上面显示的段错误.
It is generally working fine, but occassionally throws the seg fault shown above.
有什么想法吗?
推荐答案
有什么想法吗?
malloc
或 free
中的任何崩溃在 99.999% 的情况下都是其他地方早期堆损坏的结果.
Any crash inside malloc
or free
is in 99.999% of cases the result of earlier heap corruption elsewhere.
可能导致 malloc
后续崩溃的堆损坏示例:在未分配的内存上调用 free
,在某些内存上调用 free
两次指针,上溢或下溢堆分配的缓冲区,等等.
Examples of heap corruption which could lead to subsequent crash in malloc
: calling free
on a non-allocated memory, calling free
on some pointer twice, overflowing or underflowing a heap-allocated buffer, etc. etc.
查找此类错误的最快方法是:valgrind(如果在您的平台上可用)或 AddressSanitizer(在 Clang 和 GCC 的最新版本中实现).
The fastest way to find such bugs are: valgrind (if available on your platform), or AddressSanitizer (implemented in recent versions of Clang and GCC).
这篇关于localtime() 中的 malloc() 段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!