【问题标题】:c++ Crash happened when built with -xipo -xO4使用-xipo -xO4构建时发生c ++崩溃
【发布时间】:2020-05-26 17:39:34
【问题描述】:

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

我在 UNIX 上编写了一个简单的 c++ 代码。如果我用优化编译代码,它会在_Unwind_Resume 崩溃。如果我在没有优化的情况下编译它会成功运行。

代码如下所示: 日期.h

pragma once

#include <string>

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

日期.C

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

进程.h

#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

【问题讨论】:

  • 我不排除 Sun Studio 编译器中的错误。对我来说,这里没有什么问题。
  • 谢谢你,谢尔盖。我假设像你一样。
  • 尝试将文件扩展名更改为 cpp,许多编译器使用扩展名来检测语言

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


【解决方案1】:

您确定主要的 try/catch 确实在捕获吗? 按照以下建议,我最好通过参考来捕捉:

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

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

【讨论】:

  • 感谢@Benjam 的建议。我把main函数中的try catch改成const引用,还是崩溃了。
猜你喜欢
  • 2023-04-11
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多