Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在linux上捕获ostream异常? 我的Linux C++应用程序在写入字符串到OFASH对象时崩溃。 我的原始应用程序尝试创建一个非常大的字符串输出,并将所有字符串输出写入流。将字符串写入ostream对象时,应用程序崩溃。起初,崩溃发生在windows和Linux上_C++_Ostream - Fatal编程技术网

如何在linux上捕获ostream异常? 我的Linux C++应用程序在写入字符串到OFASH对象时崩溃。 我的原始应用程序尝试创建一个非常大的字符串输出,并将所有字符串输出写入流。将字符串写入ostream对象时,应用程序崩溃。起初,崩溃发生在windows和Linux上

如何在linux上捕获ostream异常? 我的Linux C++应用程序在写入字符串到OFASH对象时崩溃。 我的原始应用程序尝试创建一个非常大的字符串输出,并将所有字符串输出写入流。将字符串写入ostream对象时,应用程序崩溃。起初,崩溃发生在windows和Linux上,c++,ostream,C++,Ostream,现在,该问题已在Windows环境中修复(详细信息如下)。但在Linux中,它正在崩溃 以下是示例C++程序,它将生成相同的场景。 #include <iostream> #include <strstream> #include <memory> using namespace std; bool fillScreen(std::ostream&); int main () { auto_ptr<ostrstream> scr

现在,该问题已在Windows环境中修复(详细信息如下)。但在Linux中,它正在崩溃

以下是示例C++程序,它将生成相同的场景。

#include <iostream>
#include <strstream>
#include <memory>

using namespace std;
bool fillScreen(std::ostream&);

int main ()
{
    auto_ptr<ostrstream> screen(new ostrstream);
    bool succ = false;
    try
    {
        succ = fillScreen(*screen);
    }catch(std::exception &ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    if(succ)
    {
        std::cout << "SCREEN Content is : " << screen->str() << std::endl;
    }
    else
    {
        std::cout << "NOTHING ON SCREEN Object " << std::endl;
    }
}

bool fillScreen(ostream &scr)
{
    unsigned long idx = 0;
    scr.exceptions(std::ios::badbit);// throws exception in windows but not in Linux.
    while (idx++ < 999999999)
    {
        scr << "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH_" << " : " ;
        scr << "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_";
        scr << "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH_"<< std::endl;   
        /*if(!(idx %100000))
        {
            std::cout << "Reached iteration: " << idx << std::endl;
        }*/
    }
    return true;
}
使用此语句,我的程序不会在windows中崩溃。 在windows上运行时,流抛出badbit异常,我的应用程序处理该异常并进行干净退出

输出如下:

Windows输出:(使用cygwin运行)

清洁出口

Linux输出:

[Mybuild@devlnx01 streamError]$ ./a.out
Segmentation fault (core dumped)
[Mybuild@devlnx01 streamError]$
坠毁-不是一个干净的出口。即使有例外

screen.exceptions(std::ios::badbit);
以下是使用Linux gdb(使用核心转储文件)获取的堆栈跟踪

核心由“./a.out”生成。
程序以信号11终止,分段故障。
#在../../../../../libstdc++-v3/src/strstream.cc:174处0 std::streambuf::溢出(this=0x17f8018,c=72)
174../../../.././libstdc++-v3/src/strstream.cc:没有这样的文件或目录。
在../../../.././libstdc++-v3/src/stream.cc中
缺少单独的调试信息,请使用:debuginfo安装glibc-2.12-1.107.el6.x86_64
(gdb)在哪里
#在../../../../../libstdc++-v3/src/strstream.cc:174处0 std::streambuf::溢出(this=0x17f8018,c=72)
#std::basic_streambuf::xsputn中的1 0x00007eff6f4e7565(this=0x17f8018,uu s=,u n=72)
at/export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/streambuf.tcc:97
#2 0x00007eff6f4ddb85在sputn中(uuuu out=…,_uuus=0x401038“布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉
at/export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/streambuf:429
#3 uuu ostream_write(uuuu out=…)。。。,
__s=0x401038“布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉
at/export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:48
#4标准::\uuuuostream\uinsert(\uuuuuu out=。。。,
__s=0x401038“布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉
at/export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:99

#std::operator中的5 0x00007eff6f4dde0f这似乎是linux上随您的
gcc
副本提供的标准库的问题。 更新您的编译器(4.8.1是gcc的当前版本,截至2013年9月30日),您将获得预期的行为,如本文所示


旁注:
auto_ptr
不应再使用。使用
唯一\u ptr
共享\u ptr
。在这种情况下,两者都不是必需的,请删除
new

尝试另一个编译器,并删除auto\u ptr。这适用于现代编译器:(我刚刚复制粘贴了您的代码)。
stream
已过时,不应使用。标准头名为
sstream
。ostream现在已被弃用,我必须使用ostringstream。应用ostringstream后,程序抛出预期的错误分配并执行干净退出。。(特别感谢Alan、Lance和@DavidNorman:)。目前我无法在开发环境中使用4.8.1编译器。但我已经开始在代码中使用ostringstream。现在程序抛出异常并正确退出。
[Mybuild@devlnx01 streamError]$ ./a.out
Segmentation fault (core dumped)
[Mybuild@devlnx01 streamError]$
screen.exceptions(std::ios::badbit);
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  std::strstreambuf::overflow (this=0x17f8018, c=72) at ../../.././libstdc++-v3/src/strstream.cc:174
174     ../../.././libstdc++-v3/src/strstream.cc: No such file or directory.
        in ../../.././libstdc++-v3/src/strstream.cc
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.x86_64
(gdb) where
#0  std::strstreambuf::overflow (this=0x17f8018, c=72) at ../../.././libstdc++-v3/src/strstream.cc:174
#1  0x00007eff6f4e7565 in std::basic_streambuf<char, std::char_traits<char> >::xsputn (this=0x17f8018, __s=<value optimized out>, __n=72)
    at /export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/streambuf.tcc:97
#2  0x00007eff6f4ddb85 in sputn (__out=..., __s=0x401038 "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH_", __n=72)
    at /export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/streambuf:429
#3  __ostream_write<char, std::char_traits<char> > (__out=...,
    __s=0x401038 "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH_", __n=72)
    at /export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:48
#4  std::__ostream_insert<char, std::char_traits<char> > (__out=...,
    __s=0x401038 "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH_", __n=72)
    at /export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:99
#5  0x00007eff6f4dde0f in std::operator<< <std::char_traits<char> > (__out=...,
    __s=0x401038 "BLAHBLAHBLAH_BLAH_BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH BLAHBLAHBLAH_BLAH_")
    at /export/disk1/build/GCC4.5.3/gcc-4.5.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/ostream:513
#6  0x0000000000400d82 in fillScreen (scr=...) at overflow.cxx:35
#7  0x0000000000400c31 in main () at overflow.cxx:14