2

Could you please help to take a look into my case and give me your advice?

I wrote a simple c++ code on UNIX. If I compiled code with optimization, it got crashed at _Unwind_Resume. It run successfully if I compiled without optimization.

The code look like this: 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;
}

When I compiled with optimization, the program was crashed. The compile command is:

/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

However, with this compile command( without optimization) the program worked well:

/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

The print stack of core dump is:

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
Quentin
  • 62,093
  • 7
  • 131
  • 191
Hoai Pham
  • 21
  • 1

1 Answers1

-1

Are you sure the main try/catch is actually catching? I would better catch by reference as suggested by:

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

I suspect that you get an uncaught exception from the stream operations.

Benjam
  • 1,401
  • 2
  • 16
  • 22
  • Thank @Benjam for your advice. I changed the try catch in main function to const reference, the crash still happened. – Hoai Pham May 26 '20 at 17:58