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

问题描述 投票:2回答:1

您能帮忙研究一下我的案子并给我您的建议吗?

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

代码如下所示:date.h

pragma once

#include <string>

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

date.C

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

process.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(  );
}

process.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
c++ c++14 compiler-optimization crash-dumps
1个回答
-1
投票

您确定主要try / catch确实在捕获吗?我最好参考以下建议:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.