免费Pascal / C ++项目崩溃在cout :: sentry [英] Free Pascal/C++ project crashes in cout::sentry

查看:317
本文介绍了免费Pascal / C ++项目崩溃在cout :: sentry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合的Free Pascal / C ++项目。 Debian 5.0(Lenny)在i386上,FPC 2.4.4。当我运行程序,它崩溃的第一个 cout <调用。有趣的是,它曾经工作了一段时间;一些操作系统更新可能打破了它。以下是孤立的问题:

I have a mixed Free Pascal/C++ project. Debian 5.0 ("Lenny") on i386, FPC 2.4.4. When I run the program, it crashes on the first cout<< call. Funnily, it used to work for some time; some OS update probably broke it. Here's the issue isolated:

p.pas:

{$L c.o}    
program p;
uses initc;
procedure Hello; cdecl; external name 'Hello';

begin
     Hello;
end.     

c.cpp:

#include <iostream>
//void * __dso_handle; //You might need to uncomment that
extern "C" void Hello()
{
    std::cout << "Hello world";
}

Makefile:

p : c.o p.pas Makefile
    fpc p.pas -k-lstdc++

c.o : c.cpp
    g++ -c c.cpp

Make,run,segfault。试用了一个全新的Debian VM - 结果相同。

Make, run, segfault. Tried on a brand new Debian VM - same result.

崩溃发生在basic_fstream :: sentry :: sentry()中。他们声称这个崩溃位置与未被初始化的全局 cout 对象是一致的。这很奇怪 - 我想从Pascal使用 initc 确保全局C ++变量已初始化。

The crash takes place within basic_fstream::sentry::sentry(). They claim this crash location is consistent with the global cout object not being initialized. That's strange - I thought using initc from the Pascal side makes sure global C++ variables are initialized.

请?可能是某种程度上libstdc ++的版本我链接到它(libstdc ++。so.6.0.10)?

Any ideas, please? Could it be somehow the version of libstdc++ I'm linking against (it's libstdc++.so.6.0.10)?

编辑:它获得weirder和weirder。我在CentOS 5.3框上运行相同的二进制文件( p ) - 它的工作原理。所以可能是关于共享lib版本...我会去收集更多的统计在不同的Linuces。

it gets weirder and weirder. I run the same binary (p) on a CentOS 5.3 box - it works as advertised. So probably it's about shared lib versions... I'll go gather some more stats on different Linuces.

EDIT2:我注意到一件事:当我做 ldd p 在我的Debian框,这里是我得到:

one thing I noticed: when I do ldd p on my Debian box, here's what I get:

linux-gate.so.1 =>  (0xb77a6000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb76a6000)
libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb754b000)
libm.so.6 => /lib/i686/cmov/libm.so.6 (0xb7524000)
/lib/ld-linux.so.2 (0xb77a7000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7517000)

当我在CentOS它的工作原理:

And when I do the same on the CentOS box where it works:

libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7ec2000)
libc.so.6 => /lib/libc.so.6 (0xb7d69000)
libm.so.6 => /lib/libm.so.6 (0xb7d40000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7d34000)
/lib/ld-linux.so.2 (0xb7fb7000)

所有的C库(不是C ++)都来自 i686 / cmov 目录。 Debian机器也有 /lib/libc.so.6 ,它不同于 cmov 中的一个。与 cmov 目录有什么关系?为什么两个不同的libc副本具有相同的名称?

So all C libraries (just not the C++ one) are coming from i686/cmov directory. The Debian machine also has /lib/libc.so.6, and it's different from the one in cmov. What's the deal with that cmov directory? And why two different copies of libc with the same name?

编辑:即使在CentOS上,全局构造函数也不被调用 - 只是使用临时全局对象测试。它只是不会崩溃sentry()由于某种原因。看起来这是一个FPC问题。有关此行为的FPC中有错误报告

even on CentOS, the global constructors are not called - just tested with an ad-hoc global object. It just does not crash in sentry() for some reason. Looks like this is a FPC issue after all. There's a bug report in FPC regarding this behavior.

推荐答案

专门为 cout (以及其他全局流),添加以下行到C入口点有助于:

Specifically for cout (as well as other global streams), adding the following line to the C++ entry point helps:

std::ios_base::Init();

更大的问题仍然存在 - 未构建任意全局C ++对象。

The larger problem still exists - arbitrary global C++ objects are not constructed.

这篇关于免费Pascal / C ++项目崩溃在cout :: sentry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆