更新 2:请参阅我的代码后面的部分。
我正在使用一个线程来使用 GMP 库计算 PI,但不知何故我现在在wxThread::OnExit()
内部调用 wxWidgets 时遇到了分段错误。
这是 wxWidgets 源代码中的一行:src/msw/thread.cpp#553
这是我的线程入口函数的缩短代码:
while (i <= m_numIterations && !TestDestroy()) {
mpf_div(result, perimeter, edgeCount);
mpf_pow_ui(result, result, 2);
mpf_ui_sub(result, 1, result);
mpf_sqrt(result, result);
mpf_div_ui(result, result, 2);
mpf_sub(result, half, result);
mpf_sqrt(result, result);
mpf_mul_ui(result, result, 2);
mpf_mul(result, result, edgeCount);
mpf_set(perimeter, result);
i++;
mpf_mul_ui(edgeCount, edgeCount, 2);
}
// Free GMP variables we don't need anymore
mpf_clear(half);
mpf_clear(result);
mpf_clear(edgeCount);
// OUTPUT_DIGITS has a constant value, e.g. 12
char outputStr[OUTPUT_DIGITS];
mp_exp_t *expptr;
// If commented out, the error does not appear!
mpf_get_str(outputStr, expptr, 10, OUTPUT_DIGITS, perimeter);
更新 2:如果我用 注释掉最后一行mpf_get_str()
,则不会发生错误。
我还发现了 2003 年的一个非常古老的错误请求:http: //gmplib.org/list-archives/gmp-discuss/2003-November/000888.html
来自 GCC 调试器的调用堆栈:
#0 63AE80E9 wxThreadInternal::DoThreadOnExit(thread=0x2cfa978) (../../src/msw/thread.cpp:553)
#1 63B27ACF wxScopeGuardImpl1<void (*)(wxThread*) (../../include/wx/scopeguard.h:168)
#2 63B3F95B wxPrivate::OnScopeExit<wxScopeGuardImpl1<void (*)(wxThread*) (../../include/wx/scopeguard.h:67)
#3 63B27B36 wxScopeGuardImpl1<void (*)(wxThread*) (../../include/wx/scopeguard.h:166)
#4 63AE82FB wxThreadInternal::DoThreadStart(thread=0x2cfa978) (../../src/msw/thread.cpp:561)
#5 63AE83F2 wxThreadInternal::WinThreadStart(param=0x2cfa978) (../../src/msw/thread.cpp:602)
#6 75C4906A ui64tow() (C:\Windows\SysWOW64\msvcrt.dll:??)
#7 75C49147 msvcrt!iswalnum() (C:\Windows\SysWOW64\msvcrt.dll:??)
#8 76448543 UnregisterBadMemoryNotification() (C:\Windows\SysWOW64\kernel32.dll:??)
#9 00000000 0x02cfb178 in ??() (??:??)
#10 00000000 0x77e8ac69 in ??() (??:??)
#11 00000000 0x77e8ac3c in ??() (??:??)
#12 00000000 0x00000000 in ??() (??:??)