I'm trying to use boost::exception and have condensed my prototype code down to the example in the Boost exception tutorial however when running the code with the BOOST_THROW_EXCEPTION macro I get an abort from the program.
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
Running the code with the BOOST_THROW_EXCEPTION macro:
$ ./a.out
Abort trap
If as the comment says, I swap the code, all is well
$ ./a.out
My info: 42
Below is the output from the g++ preprocessor for f()
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
Software versions are:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost #1.47.0 devel/boost
I'm on OSX SL using the tools provided by MacPorts. I've double checked the g++ search paths and there's only one copy of the boost hpp files and that's the ones that belong to the aforementioned boost package.
I have no idea why the abort trap is being called. I admit I'm newish to C++ .
The problem was caused by using the MacPorts version of g++. There are plenty of tickets related to exceptions and Abort Traps in the MP system (and plenty of examples on Google).
Using the version of g++ that comes with XCode enabled this problem to go away.
Related
I can't figure why the following instruction crashes:
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
I got the following error: Process returned -1073741819 (0xC0000005)
There is nothing more to catch regarding exceptions and AFAIK the boost documentation doesn't mention incompatibility issues between the boost and openssl versions.
my environment:
gcc from cygwin: C:\cygwin64\x86_64-w64-mingw32-g++.exe
linker options: -lws2_32 -lcrypto -lssl
using boost 1.78 (dl from website) and cygwin's openssl 1.1.1m packages
here is the minimal example:
#include <iostream>
#include <boost/asio/ssl/context.hpp>
int main()
{
std::cout << "before" << std::endl;
try {
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
} catch (...) {
std::cout << "catch" << std::endl;
}
std::cout << "after" << std::endl;
return 0;
}
output:
before
The openssl cygwin package I installed is not a stable one, so the include and lib files are missing and I'm using the wrong ones (incompatible with the x86_64-w64-mingw32-g++ compiler). I've installed another stable version and the desired files are available now.
I keep getting terminate called for anything I throw using GCC 9.2, even if it is caught.
terminate called after throwing an instance of 'char const*'
terminate called recursively
I have tested -std=c++17, -std=c++14, -std=c++11
Example test:
#include <iostream>
int main()
{
try
{
throw "not found";
}
catch(...)
{
std::cout << "test" << std::endl;
}
return 0;
}
It doesn't fail if i compile using visual studio or on multiple of the online compilers.
example:
https://repl.it/languages/cpp
https://www.onlinegdb.com/online_c++_compiler
I have also tried putting the throw in a function and adding noexcept(false), but this fails as well. Example:
#include <iostream>
void foo() noexcept(false)
{
throw std::runtime_error( "test1" );
}
int main()
{
try
{
foo();
}
catch(...)
{
std::cout << "test2" << std::endl;
}
return 0;
}
Edit:
System info:
I'm using 9-2020-q2-update - arm-linux-none-gnueabihf.
Basically, the setup is Linux x86 as my main computer, cross compiling for ARM Cortex-A processor. The processor i'm testing are Raspberry Pi 4 and BeagleBone Black.
The code compiles correctly and runs fine on the target processor, except when an exception is hit. At which point, it hits terminate for any throw.
I'm using Eclipse as the IDE, using remote debug to upload and step through the code on either of the target processors.
It seems there is a bug or exception handling isn't working on version 9.2 of GCC (ARM only?) compiler.
I tried with version 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 compiler and they are working just fine. No other changes were necessary, other than the compile switch.
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
I'm trying to compile an easy program that use the alias declaration.
This is the code:
#include <iostream>
using namespace std;
using in = int;
in main ()
{
in a = 1;
cout << a << '\n';
return 0;
}
The command I use to compile is g++ -std=c++0x program_name.cxx, using the built-in terminal in Kate on Ubuntu OS.
But it doesn't work! Any suggestion?
(instead using typedef int in; it works).
Compile in C++11 mode. Type aliasing is supported only in C++11. I suspect the g++ version that use is older and doesn't fully support c++11, hence fails with c++0x.
Compile with: g++ -std=c++11 file.cpp
and it works.
By the way, it seems to be a terrible idea to alias int in such a way.
I'm using C++ Primer 5th to learn C++. The code below is copied from P729.
#include <iostream>
#include <regex>
#include <string>
int main()
{
// find the characters ei that follow a character other than c
std::string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
std::regex r(pattern);
// ~~~~~~~~~~~^~~~~~~~~~~ where the exception was thrown.
std::smatch results;
std::string test_str = "receipt freind theif receive";
if (regex_search(test_str, results, r))
std::cout << results.str() << std::endl;
return 0;
}
When running it, an exception was thrown :
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Press <RETURN> to close this window...
By debugging step by step, I found it was thrown while constructing the object r from the code from bits/regex_compiler.h:
template<typename _InIter, typename _TraitsT>
bool
_Compiler<_InIter, _TraitsT>::
_M_bracket_expression()
{
if (_M_match_token(_ScannerT::_S_token_bracket_begin))
{
_RMatcherT __matcher(_M_match_token(_ScannerT::_S_token_line_begin),
_M_traits);
if (!_M_bracket_list(__matcher)
|| !_M_match_token(_ScannerT::_S_token_bracket_end))
__throw_regex_error(regex_constants::error_brack);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_M_stack.push(_StateSeq(_M_state_store,
_M_state_store._M_insert_matcher(__matcher)));
return true;
}
return false;
}
OK.The code above is totally beyond what I can understand.That's what I have tried so far.Can anyone tell me what's gong on here? How to fix it?
UPDATE:
The compiler I'm using:
gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
You need to use minimal version of g++ 4.9 to use regex properly.(You can compile it with lower versions, but it's broken)
You can download g++ 4.9 from their side. GCC SITE
After that try:
g++49 -std=c++0x -static-libstdc++.
I try to search more about it for you.
EDIT: g++49, because of the reason, that version 4.9 can't be default after building it.
i have a code like this
#include <iostream>
#include <string>
using namespace std;
void Test(){
string line;
}
int main(){
cout << "test " << endl;
return 0;
}
The code is compilable but when i try to run it, the program stop working. Then use gdb to discover what wrong with my program
(gdb) run
Starting program: E:\CPP\Program dinamis\a.exe
[New Thread 4892.0x1d4c]
test
Program received signal SIGILL, Illegal instruction.
0x6fcc43c3 in libstdc++-6!_ZSt4cout () from C:\MinGw\bin\libstdc++-6.dll
i dont understand what wrong with it. i use MinGW (G++) as my compiler by typing g++ -v :
E:\CPP\Program dinamis>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-
languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2
--enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-
runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)
but if i write the same code on my visual studio, the program runs well without error. what should i do with my G++
You are not actually using the test function for output, you could use it as follows but not sure why there is any error appearing for you as for me it runs fine under GNU GCC version 4.8.1
Here is a sample of code which uses Test()
#include <iostream>
#include <string>
using namespace std;
string Test(){
string line = "My text Line here!";
return line;
}
string Test2(){
return "My text Line2 here!";
}
int main(){
cout << Test() << endl;
cout << Test2() << endl;
return 0;
}
The second test is to illustrate a simpler usage for returning a string, notee as well the function needs to be defined as returning a string as well.
Well finally i found the solution. I am running on windows 8 Operating system and it seems the main problem is on linking process while compiling the source code.
So let say the filename is mycode.cpp and i just run the command with additional -static-libgcc -static-libstdc++ options like this one:
g++ "mycode.cpp" -o mycode -static-libgcc -static-libstdc++
Just a blind shot, try handling main function parameters:
int main(int, char**)
{
//...
Maybe it's something with the stack...?