这可能是什么? [程序错误]
我在解决这个问题时遇到了一些麻烦..所以是时候寻求帮助了。
操作系统:OSX、Snow Leopard
gcc:gcc 的 Xcode 版本,4.2.1
库:gmp、gmpxx
问题:函数在最后崩溃:
flx_run(94749) malloc: *** error for object 0x7fff706d1500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted.
0x00007fff835cd886 in __kill ()
(gdb) bt
#0 0x00007fff835cd886 in __kill ()
#1 0x00007fff8366deae in abort ()
#2 0x00007fff83585a75 in free ()
#3 0x00000001000bca45 in flxusr::gmp__hyphen_0::_init_ ()
Previous frame inner to this frame (gdb could not unwind past this frame)
我没有在函数 init 中调用 free() init,并且该函数确实正确运行到右大括号..因此崩溃点必须位于本地对象的析构函数中(对吗?)
有趣的是:
a)该程序(似乎)工作正常,如果静态链接,只有当代码内置到 *.dylib 并动态加载时才会失败。
b) 对于两个完全不同版本的 gmp/gmpxx,程序以同样的方式失败。 [4.3 或其他版本和 5.01]
c) 静态和动态链接版本在 Ubuntu 10.5 LTS 上都可以正常工作
该代码是由我的编译器生成的,它不是手写代码。用户代码由顶级驱动程序运行。该系统成功运行了数百次回归测试。然而,在这个特定的测试用例中使用了以下手写代码:
namespace flx { namespace gmp {
string flx_mpf_get_str(mpf_class const &a)
{
mp_exp_t x;
std::string s = a.get_str(x,10,0);
char b[100];
std::snprintf(b,100,"e%ld",(long)x);
return std::string(".")+s+b;
}
}}
但它对我来说看起来不错。该函数的堆栈帧包含几个 mpz_class、mpq_class 和 mpf_class 对象。
除了 gmp/gmpxx 中的错误之外,我唯一能想到的是我的运行时系统或生成的代码正在破坏内存。未分配的被“释放”的地址是可疑的(它远离堆)。我用 -g 标志构建了一个 gmp 版本,但没有得到更多信息(这表明它从 gmpxx 类析构函数中释放出来并不是坏事)。
我需要一些想法!我现在不知道该去哪里看。提前谢谢:)
链接到源
http://felix-lang.org:1116/$ /home/skaller/felix/felix/build/release-optimized/test/gmp/gmp-0.cpp
元:SO 的降价不起作用!所以你必须手动复制。
不妨添加 Felix 代码的链接,因为它更具可读性,并且其中有一些有趣的东西(例如用户定义的语法,如果您访问库,您可以看到正在实例化的类型类)
http://felix-lang.org:1116/$/home/skaller/felix/felix/build/release-optimized/test /gmp/gmp-0.flx
I am having some trouble figuring this out .. so time to ask for help.
OS: OSX, Snow Leopard
gcc: Xcode version of gcc, 4.2.1
Library: gmp, gmpxx
Problem: a function is crashing right at the end with:
flx_run(94749) malloc: *** error for object 0x7fff706d1500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted.
0x00007fff835cd886 in __kill ()
(gdb) bt
#0 0x00007fff835cd886 in __kill ()
#1 0x00007fff8366deae in abort ()
#2 0x00007fff83585a75 in free ()
#3 0x00000001000bca45 in flxusr::gmp__hyphen_0::_init_ ()
Previous frame inner to this frame (gdb could not unwind past this frame)
I am not calling free() in the function init, and the function does run correctly right up to the closing curly brace .. so the crash point has to be in the destructors of local objects (right?)
The funny thing is:
a) The program (seems to) work fine if statically linked, it only fails if the code is built into a *.dylib and dynamically loaded.
b) the program fails the same way with two completely different versions of gmp/gmpxx.
[4.3 or something and 5.01]
c) both static and dynamic linkage versions work fine on Ubuntu 10.5 LTS
The code is generated by my compiler, it is not hand written code. The user code is run by a top level driver program. The system runs several hundred regression tests successfully. however the following hand written code is used in this particular test case:
namespace flx { namespace gmp {
string flx_mpf_get_str(mpf_class const &a)
{
mp_exp_t x;
std::string s = a.get_str(x,10,0);
char b[100];
std::snprintf(b,100,"e%ld",(long)x);
return std::string(".")+s+b;
}
}}
but it looks fine to me. The stack frame of the function contains several mpz_class, mpq_class and mpf_class objects.
Apart from bugs in gmp/gmpxx the only other thing I can think of is that my run time system or generated code is corrupting memory. The address being "freed" which wasn't allocated is suspicious (it's nowhere near the heap). I built a version of gmp with -g flag but get no more information (which suggests it isn't a bad free from inside a gmpxx class destructor).
I need some ideas! I have no idea where to look now. Thx in advance :)
Link to the source
http://felix-lang.org:1116/$/home/skaller/felix/felix/build/release-optimized/test/gmp/gmp-0.cpp
Meta: SO's markdown doesn't work! So you'll have to copy by hand.
Might as well throw in the link to the Felix code since it's a bit more readable and has some fun stuff in it (like user defined syntax and if you go to the library you can see typeclasses being instantiated)
http://felix-lang.org:1116/$/home/skaller/felix/felix/build/release-optimized/test/gmp/gmp-0.flx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你从哪里得到gmp源码?它可能是 32 位版本(至少 macports 不提供 64 位源)。另一方面,Snow Leopard 是 64 位(默认情况下)。
就我个人而言,我通过 gcc -m32 强制所有代码为 32 位。你也可以尝试一下。
br,
Juha
编辑: 如何强制 /usr/bin/gcc -> /usr/bin/gcc -m32?
Where did you get the gmp source? It might be a 32 bit version (at least macports does not offer 64 bit sources). Snow Leopard on the other hand is 64 bit (by default).
Personally I force all code to 32 bit by gcc -m32. You could try the same.
br,
Juha
edit: How to force /usr/bin/gcc -> /usr/bin/gcc -m32?
您无法释放动态库分配的内存。也许这就是问题所在。
查看此答案:共享库内存空间
You can't deallocate memory allocated by a dynamic library. Perhaps that is the problem here.
Check out this answer: Shared libraries memory space