c++ - 使用-xipo -xO4构建时发生c++崩溃

标签 c++ c++14 compiler-optimization crash-dumps

您能帮我看看我的案子并给我您的建议吗?

我在UNIX上编写了一个简单的c++代码。如果我通过优化编译代码,则它在_Unwind_Resume崩溃。如果我不经过优化就编译,它将成功运行。

代码如下:
date.h

pragma once

#include <string>

namespace Test
{
class Date
{    
 public:
  std::string asString() const;
};
}

日期
#include "date.h"
std::string Test::Date::asString() const
{    
  return std::string();
}

进程
#pragma once

namespace Test
{
class Date;
class Process
{
public:
  void operator()(  );
private:
  void attemptProcessing( const Test::Date& );
  void firstValidation(  const Test::Date& );
  void secondValidation( const Test::Date& );
};

void doPostingProcess(  );
}

过程C
#include <exception>
#include <iostream>
#include <sstream>

/*------------------------------------------------------------------------*/

void Test::Process::operator()()
{
  try {

    Test::Date date;
    attemptProcessing( date );

  } catch ( const std::exception& e ) {
    std::cout << "ex: " << e.what() << std::endl;
  }

}

void Test::Process::attemptProcessing(  const Test::Date& d)
{

    std::cout << "If remove this, crash does not happen: " << d.asString() << std::endl;
    firstValidation(  d );
}

void Test::Process::firstValidation( const Test::Date& d )
{
  secondValidation(  d );
}
void Test::Process::secondValidation( const Test::Date& d )
{
  std::ostringstream o;

  o << "If remove this, crash does not happen";

  throw std::exception();
}

void Test::doPostingProcess(  )
{
 auto processAccount = Test::Process();
 processAccount();
}

test_validation.C
#include "process.h"
#include "date.h"

#include <iostream>

#include "process.h"

int main( int argc, char const* argv[] )
{
  try {
    Test::doPostingProcess( );
  } catch( std::exception ex ) {
    std::cout << ex.what() << std::endl;
  }
  return 0;
}

当我进行优化编译时,程序崩溃了。编译命令为:
/opt/developerstudio12.6/bin/CC -std=c++14   -xipo -xO4 +w -xildoff -D_POSIX_PTHREAD_SEMANTICS -g0 -xtarget=generic -xcache=generic -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  date.C process.C test_validation.C

但是,使用此编译命令(未经优化),程序运行良好:
/opt/developerstudio12.6/bin/CC -std=c++14    +w -xildoff -D_POSIX_PTHREAD_SEMANTICS -g0 -xtarget=generic -xcache=generic -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  date.C process.C test_validation.C

核心转储的打印堆栈为:
core '/var/corefiles/core.dc2nix1d3v10.a.out.28170' of 28170:   a.out
 fedaec9c _lwp_kill (6, 0, 0, fed8e13c, ffffffff, 6) + 8
 fed229c8 abort    (2, 1, fef9f068, ffb40, fee25518, 0) + 110
 fef8c22c _Unwind_Resume (22db8, 0, ff150e58, ff150b5c, ffbfd944, ffbfd718) + 58
 000122b8 main     (22c00, 12800, ffbfdcf4, 22db8, 20, ffbfdcf3) + 488
 00011728 _start   (0, 0, 0, 0, 0, 22870) + 108

最佳答案

您确定主要的try / catch实际上是在捕获吗?
我最好引用一下:

C++ catch blocks - catch exception by value or reference?

我怀疑您从流操作中得到了一个未捕获的异常。

关于c++ - 使用-xipo -xO4构建时发生c++崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62028144/

相关文章:

c++ - 使用重载的 << 运算符打印嵌套 vector 的内容

c++ - 模板作为函数模板的参数 - 推导失败

c++ -/Ox 和/O2 编译器选项有什么区别?

c++ - 像素路径性能警告 : Pixel transfer is synchronized with 3D rendering

c++ - 运行时使用的自定义调试 printf

c++ - lambda tbb parallel reduce with a few arguments

c++ - "Expected unqualified-id"将我的代码设为模板时出现错误,而在将其设为模板之前没有出现该错误?

c - 为什么此代码会导致浮点异常 - SIGFPE

asp.net - web.config中system.codedom中多个编译器定义的影响

c++ - 将结构传递给 vector ,打印 vector 会产生奇怪的结果