gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:03:42 25 4
gpt4 key购买 nike

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

我在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com